当前位置:学Delphi网文档资料专题讲解JSON

JSON学习---Json数据的增加,删除,修改

减小字体 增大字体 作者:万一  来源:万一的博客  发布时间:2010-10-15 23:22:55

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses SuperObject;

//添加
procedure TForm1.Button1Click(Sender: TObject);
var
  jo: ISuperObject;
  i: Integer;
begin
  jo := TSuperObject.Create;

  for i := 0 to 9 do jo.I['N' + IntToStr(i)] := i;
  ShowMessage(jo.AsJSon(True));

  for i := 0 to 9 do jo.S['N' + IntToStr(i)] := IntToStr(i);
  ShowMessage(jo.AsJSon(True));

  for i := 0 to 9 do jo.B['N' + IntToStr(i)] := Odd(i);
  ShowMessage(jo.AsJSon(True));

  for i := 0 to 9 do jo.D['N' + IntToStr(i)] := i / 2;
  ShowMessage(jo.AsJSon(True));

  for i := 0 to 9 do jo.O['N' + IntToStr(i)] := SO('{"A":"B"}');
  ShowMessage(jo.AsJSon(True));

  jo.Clear;
  ShowMessage(jo.AsJSon(True));
end;

//修改与删除
procedure TForm1.Button2Click(Sender: TObject);
var
  jo: ISuperObject;
  str: string;
begin
  jo := SO('{"Name":"张三", "Age":88}');
  ShowMessage(jo.AsJSon(False, False));

  jo.S['Name'] := '李四';
  jo.I['Age'] := 18;
  ShowMessage(jo.AsJSon(False, False));

  jo.Delete('Age');
  ShowMessage(jo.AsJSon(False, False));

  jo.D['Size'] := 1.83;
  ShowMessage(jo.AsJSon(False, False));
end;

//TSuperArray 的增、删、改与查看
procedure TForm1.Button3Click(Sender: TObject);
var
  jo,item: ISuperObject;
  ja: TSuperArray;
  i: Integer;
  str: string;
begin
  jo := SO('{"爱好":["编程","打球"]}');
  ShowMessage(jo.AsJSon(False, False));

  jo.A['爱好'].Add(SO('看电视'));
  ShowMessage(jo.AsJSon(False, False));

  ja := jo.A['爱好'];
  ja.Add(SO('游泳'));
  ShowMessage(jo.AsJSon(False, False));

  ja.Insert(0, SO('睡觉'));
  ShowMessage(jo.AsJSon(False, False));

  ja.Delete(0);
  ShowMessage(jo.AsJSon(False, False));

  ja.D[0] := 3.14;
  ShowMessage(jo.AsJSon(False, False));

  ja.O[0] := SO('{}');
  ShowMessage(jo.AsJSon(False, False));

  str := '';
  for i := 0 to ja.Length - 1 do str := Format('%s %s', [str, ja.S[i]]);
  ShowMessage(Trim(str));

  str := '';
  for item in jo['爱好'] do str := Format('%s %s', [str, item.AsString]);
  ShowMessage(Trim(str));

  ja.Clear;
  ShowMessage(jo.AsJSon(False, False));
end;

end.

Tags:Json增、删、改

作者:万一
  • 好的评价 如果您觉得此文章好,就请您
      0%(0)
  • 差的评价 如果您觉得此文章差,就请您
      0%(0)

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论