delphi JsonDataObjects


本文整理自网络,侵删。

 

特征

快速双重JSON解析器,无需转换即可解析UTF8和UTF16

自动创建数组和对象

带有隐式运算符的轻松访问模式

紧凑和格式化的输出模式

变体支持

如果JsonSerializationConfig.NullConvertsToValueTypes设置为True,则可以将Null自动转换为值类型。

进度回调支持,用于加载大型JSON字符串

Win32,Win64和ARM Android支持(MacOS和iOS可以工作)


用法:

Simple example


var

  Obj: TJsonObject;

begin

  Obj := TJsonObject.Parse('{ "foo": "bar", "array": [ 10, 20 ] }') as TJsonObject;

  try

    ShowMessage(Obj['foo']);

    ShowMessage(IntToStr(Obj['array'].Count));

    ShowMessage(IntToStr(Obj['array'].Items[0]));

    ShowMessage(IntToStr(Obj['array'].Items[1]));

  finally

    Obj.Free;

  end;

end;

Filling and serializing JSON objects


var

  Obj, ChildObj: TJsonObject;

begin

  Obj := TJsonObject.Create;

  try

    // easy access

    Obj['foo'] := 'bar';

    // normal (and faster) access

    Obj.S['bar'] := 'foo';

    // automatic array creation, Obj is the owner of 'array'

    Obj.A['array'].Add(10);

    Obj.A['array'].Add(20);

    // automatic object creation, 'array' is the owner of ChildObj

    ChildObj := Obj.A['array'].AddObject;

    ChildObj['value'] := 12.3;

    // automatic array creation, ChildObj is the owner of 'subarray'

    ChildObj.A['subarray'].Add(100);

    ChildObj.A['subarray'].Add(200);


    ShowMessage(Obj.ToJSON({Compact:=}False));

  finally

    Obj.Free;

  end;

{

"foo": "bar",

"bar": "foo",

"array": [

10,

20,

{

"value": 12.3,

"subarray": [

100,

200

]

}

]

}

Copying JSON objects with Assign


var

  Obj, ClonedObj: TJsonObject;

begin

  Obj := TJsonObject.ParseUtf8('{ "foo": [ "bar", {}, null, true, false, { "key": "value" } ] }') as TJsonObject;

  try

    ClonedObj := TJsonObject.Create;

    try

      // Make a copy of Obj

      ClonedObj.Assign(Obj);

      ShowMessage(ClonedObj.ToJSON(False));

    finally

      ClonedObj.Free;

    end;

  finally

    Obj.Free;

  end;

end;

{

"foo": [

"bar",

{},

null,

true,

false,

{

"key": "value"

}

]

}


JsonDataObjects.pas 单元文件下载地址

https://github.com/ahausladen/JsonDataObjects

相关阅读 >>

Delphi 如何通过代码控制打开键盘数字锁定numlock

Delphi2010中Delphi class explorer妙用

Delphi2010读取mysql数据库text类型乱码的解决方案

Delphi用命令行加载驱动

Delphi 每年、月、周、日的开始与结束的时间

winapi 字符及字符串函数(14): chartooem、oemtochar

Delphi 另类计算程序代码运行耗时

Delphi memo 字符串换行

Delphi 通过twebbrowser文档中的id查找html元素

Delphi 回车 选择下一个控件

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...