利用SuperObject 循环处理Json深层次的值
//遍历对象 procedure TForm1.Button5Click(Sender: TObject); var item,jo: ISuperObject; ja,JA_TYPE,JA_MAC:TSuperArray; //json数组 i,j,k:Integer; jts: TSuperTableString; //TSuperTableString 的常用属性: count、GetNames、GetValues begin jo := SO(Memo1.Text); jts:=jo.AsObject; mmo1.Lines.Add(‘结点数:‘+IntToStr(jts.count )); mmo1.Lines.Add(‘键名:‘+jts.GetNames.AsString); mmo1.Lines.Add(‘键值:‘+jts.GetValues.AsString); jts:=jo[‘addresses‘].AsObject; mmo1.Lines.Add(‘结点数:‘+IntToStr(jts.count )); mmo1.Lines.Add(jts.GetNames.AsString); mmo1.Lines.Add(‘键值:‘+jts.GetValues.AsString); jts:=jo.A[‘children‘].N[0].AsObject; mmo1.Lines.Add(‘结点数:‘+IntToStr(jts.count )); mmo1.Lines.Add(jts.GetNames.AsString); mmo1.Lines.Add(‘键值:‘+jts.GetValues.AsString); jts:=jo.A[‘children‘].N[0].A[‘M_TYPE‘].N[0].AsObject; mmo1.Lines.Add(‘结点数:‘+IntToStr(jts.count )); mmo1.Lines.Add(jts.GetNames.AsString); mmo1.Lines.Add(‘键值:‘+jts.GetValues.AsString); jts:=jo.A[‘children‘].N[0].A[‘M_TYPE‘].N[0].A[‘MACHINE‘].N[0].AsObject; mmo1.Lines.Add(‘结点数:‘+IntToStr(jts.count )); mmo1.Lines.Add(jts.GetNames.AsString); mmo1.Lines.Add(‘键值:‘+jts.GetValues.AsString); ja:=jo.A[‘children‘]; //等同 ja:=jo.O[‘children‘].AsArray; for i := 0 to ja.Length-1 do begin mmo1.Lines.Add(‘店主:‘+ja.O[i].s[‘name‘]); //区分大小写 JA_TYPE:=ja.O[i].O[‘M_TYPE‘].AsArray; for j := 0 to ja_type.Length-1 do begin mmo1.Lines.Add(‘出票类型:‘+IntToStr(JA_TYPE.O[j].I[‘MACHINE_TYPE‘])); mmo1.Lines.Add(‘出票类型名:‘+JA_TYPE.O[j].s[‘MACHINE_NAME‘]); JA_MAC:=JA_TYPE.O[j].O[‘MACHINE‘].AsArray; for k := 0 to ja_mac.Length-1 do begin mmo1.Lines.Add(‘机器编号:‘+JA_MAC.O[k].s[‘MACHINE_NO‘]); mmo1.Lines.Add(‘机器状态:‘+IntToStr(JA_MAC.O[k].I[‘STATUS‘])); mmo1.Lines.Add(‘机器余额:‘+FloatToStr(JA_MAC.O[k].D[‘BALANCE‘])); end; end; end; // for item in jo do ShowMessage(item.AsJSon(False, False)); end;
//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,
- "M_TYPE": [
- {
- "MACHINE_TYPE": 1,
- "MACHINE_NAME": "传统",
- "MACHINE": [ { "MACHINE_NO": "26088", "STATUS": 2, "BALANCE": 12.3 } ]
- },
- {
- "MACHINE_TYPE": 2,
- "MACHINE_NAME": "竞彩",
- "MACHINE": [{ "MACHINE_NO": "26093", "STATUS": 1, "BALANCE": 100.22 } ]
- }
- ]
- },
- {
- "name": "张承",
- "age": "11",
- "sex": false,
- "M_TYPE": [
- {
- "MACHINE_TYPE": 3,
- "MACHINE_NAME": "传统",
- "MACHINE": [{"MACHINE_NO": "26038", "STATUS": 3, "BALANCE": 1300.5} ]
- }
- ]
- }
- ]
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。