JsonDataObjects 简单实用

下载地址https://github.com/ahausladen/JsonDataObjects

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"
        }
    ]
}

 

转载于:https://www.cnblogs.com/win32pro/p/5349474.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值