Delphi XE7中使用JSON

2018-10-30

Delphi XE7有一个对JSON处理的单元,在你需要使用JSON的单元里面引入"System.json",随后你就可以用Delphi自己的json处理类了。我写的小例子只是对包含字符串和数组的JSON进行解析,这两种数据类型,我觉得是实际使用中最常用、有用的类型,所以我仅仅用这两种例子做演示!

演示代码:
{
  功能:DelphiXE7中使用JSON
  ------------------------------------------------------------------------------
  说明:
  1,使用Delphi自己带的JSON(system.json)。
  2,这仅仅是一个简单例子,以后还会增加演示功能。
  ------------------------------------------------------------------------------
  注意:
  1,JSON类创建后,里面所有元素不用管释放,JSON类自己管理,千万不要画蛇添足啊!!!!!!
  ------------------------------------------------------------------------------
  作者:孙玉良 QQ:14667479 Email:sunylat@163.com  修改时间:2014/11/23 00:13
  ------------------------------------------------------------------------------
  开发工具:Delphi XE7
  测试手机:华为荣耀X1
}
unit Unit1;


interface


uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  FMX.Layouts, FMX.Memo;


type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Memo1: TMemo;
    Panel2: TPanel;
    Button1: TButton;
    Button2: TButton;
    Memo2: TMemo;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    { Private declarations }


    // 重新设置button按钮
    procedure ResetButton;
  public
    { Public declarations }
  end;


var
  Form1: TForm1;


const
  // 演示用的JSON
  jsonString = '{"name":"张三", "other":["中国","程序员"]}';


implementation


{$R *.fmx}


uses
  System.json; // Dephi自带的JSON单元


procedure TForm1.Button1Click(Sender: TObject);
var
  JSONObject: TJSONObject; // JSON类
  i: Integer; // 循环变量
  temp: string; // 临时使用变量
  jsonArray: TJSONArray; // JSON数组变量
begin


  if Trim(Memo1.Text) = '' then
  begin
    ShowMessage('要解析数据不能为空!');
  end
  else
  begin
    JSONObject := nil;
    try
      { 从字符串生成JSON }
      JSONObject := TJSONObject.ParseJSONValue(Trim(Memo1.Text)) as TJSONObject;


      if JSONObject.Count > 0 then
      begin


        { 1,遍历JSON数据 }
        Memo2.Lines.Add('遍历JSON数据:' + #13#10);


        Memo2.Lines.Add('JSON数据数量:' + IntToStr(JSONObject.Count));


        for i := 0 to JSONObject.Count - 1 do
        begin


          if i = 0 then
          begin
            temp := JSONObject.Get(i).ToString + #13#10;;
          end
          else
          begin
            temp := temp + JSONObject.Get(i).ToString + #13#10;
          end;


        end;


        { output the JSON to console as String }
        Memo2.Lines.Add(temp);


        Memo2.Lines.Add('------------------------------');


        { 2,按元素解析JSON数据 }
        Memo2.Lines.Add('按元素解析JSON数据:' + #13#10);
        temp := 'name = ' + JSONObject.Values['name'].ToString + #13#10;
        Memo2.Lines.Add(temp);


        // json数组
        jsonArray := TJSONArray(JSONObject.GetValue('other'));;
        if jsonArray.Count > 0 then
        begin


          // 得到JSON数组字符串
          temp := 'other = ' + JSONObject.GetValue('other').ToString + #13#10;


          // 循环取得JSON数组中每个元素
          for i := 0 to jsonArray.Size - 1 do
          begin
            temp := temp + IntToStr(i + 1) + ' : ' + jsonArray.Items[i]
              .Value + #13#10;
          end;


        end;


        Memo2.Lines.Add(temp);


      end
      else
      begin
        temp := '没有数据!';
        Memo2.Lines.Add(temp);
      end;


    finally
      JSONObject.Free;
    end;
  end;


end;


// 清空显示数据
procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo1.Text := '';
  Memo2.Text := '';
end;


// 设置要解析的JSON数据
procedure TForm1.Button3Click(Sender: TObject);
begin
  Memo1.Text := jsonString;
end;


// 设置要解析的JSON数据
procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Text := jsonString;
end;


procedure TForm1.FormResize(Sender: TObject);
begin
  // 重新设置button按钮
  self.ResetButton;
end;


// 重新设置button按钮
procedure TForm1.ResetButton;
var
  buttonWidth: Integer;
begin
  buttonWidth := self.Width div 3;


  Button1.Width := buttonWidth;
  Button2.Width := buttonWidth;
  Button3.Width := buttonWidth;
end;


end.

阅读44