SuperObject使用
SuperObject
SuperObject第三方控件包,试用了下并编写了个DEMO,发现还是蛮强大。废话 少说,看看下面的代码就知道其强大:
1、JSON原始数据
{
"name": "张三", /* 注释 */
"age": 33,
"sex": true,
"weight": 123.456,
"tel": ["86-1111111", "86-2222222"],
"addresses":{"address":"A省B市", "pc":"100001"},
"children": [
{
"name": "张继",
"age": "22",
"sex": true
},
{
"name": "张承",
"age": "11",
"sex": false
}
]
}
2、获取某个域内容,比如名称,代码如下:
var
jo: ISuperObject;
begin
jo := TSuperObject.ParseString(PWideChar(mmo.Text),false);
ShowMessage('name:'+jo['name'].AsString);
//也可以采用以下方式实现
//jo := SO(PWideChar(mmo.Text));
//ShowMessage('message:'+jo['message'].AsString);
end;
3、获取嵌套对象,比如地址和邮编
var
jo: ISuperObject;
begin
jo := TSuperObject.ParseString(PWideChar(mmo.Text),false);
ShowMessage('地址:'+jo['addresses.address'].AsString+',邮编:'+jo['addresses.pc'].AsString);
end;
4、解析子记录
var
i: integer;
jo: ISuperObject;
ja: TSuperArray;
begin
jo := TSuperObject.ParseString(PWideChar(mmo.Text),false);
ShowMessage('记录:'+jo['children'].AsString);
ja := jo['children'].AsArray;
for i:=0 to ja.Length-1 do
ShowMessage(ja['name'].AsString);
end;