Delphi / SuperObject - Accessing Subnodes

2019-04-28 23:10发布

I have the following JSON from my server:

{
   "userid":"12",
   "username":"TestChar",
   "logged":"yes",
   "status":"Premium User",
   "areas":{
      "SERVICEAREA_XX1":{
         "id":"1",
         "area":"SERVICEAREA_XX1",
         "version":"3000",
         "usr_group":"0"
      },
      "SERVICEAREA_XX2":{
         "id":"2",
         "area":"SERVICEAREA_XX2",
         "version":"31000",
         "usr_group":"0"
      },
      "SERVICEAREA_XX3":{
         "id":"3",
         "area":"SERVICEAREA_XX3",
         "version":"2000",
         "usr_group":"1"
      }
   }
}

With SuperObjects i can get the count of "SERVICEAREA"'s with

ob['areas'].AsObject.count

How can i now get access to the elements of the different "SERVICEAREA"'s?

Thanks for your help...

3条回答
Root(大扎)
2楼-- · 2019-04-28 23:39

you can access elements of an array using a for ... in loop:

var
  item: ISuperObject;
begin
  for item in ob['areas'] do ...

or without an enumerator, using a 'normal' for loop:

var
  idx: Integer;
  item: ISuperObject;
begin
  for idx := 0 to ob['areas'].AsArray.Length - 1 do
    item := ob['areas'].AsArray[idx];
查看更多
姐就是有狂的资本
3楼-- · 2019-04-28 23:48

use this code If you want to access key/value(like Javascriptfor..in)

 if ObjectFindFirst(JsonData, ite) then
    with JsonData.AsObject do
    repeat
      PutO(ite.key, ite.val.Clone);
    until not ObjectFindNext(ite);
    ObjectFindClose(ite);
查看更多
beautiful°
4楼-- · 2019-04-28 23:55

Marjan has the answer for you. Here is a little more information how to access the item properties with an example:

var
  item: ISuperObject;
...
for item in ob['areas'] do
begin
  WriteLn(item['id'].AsInteger);
  WriteLn(item['area'].AsString);
  WriteLn(item['version'].AsInteger);
end;
查看更多
登录 后发表回答