XSuperObject对象显示

时间:2014-10-15 09:09:13

标签: json delphi

我有一行json类型:

Rows : [{ scan : 12, find : 6, fImg : 2 }]

我正在使用xsuperobject。
我想展示Rows.scan
如果它不是一个物体,我可以像X ['scan']那样显示;

我搜索了这个页面:https://code.google.com/p/x-superobject/source/browse/wiki/Sample.wiki

*Sample 2*
{{{
const
  JSN = '{ '+
        ' "adresses": [ '+
        '   { '+
        '     "adress": "blabla", '+
        '     "city": "Antalya", '+
        '     "pc": 7160 '+
        '   },'+
        '   { '+
        '     "adress": "blabla", '+
        '     "city": "Adana", '+
        '     "pc": 1170 '+
        '   } '+
        ' ] '+
        '}';
var
  X, Obj: ISuperObject;
  J: Integer;
begin
  X := TSuperObject.Create(JSN);
  with X.A['adresses'] do
    for J := 0 to Lenght -1 do
    begin
      Obj := O[J];
      Obj.First;
      while not Obj.EoF do
      begin
         Memo1.Lines.Add( Obj.CurrentKey + ' = ' + VarToStr(Obj.CurrentValue.AsVariant));
         Obj.Next;
      end;
      Memo1.Lines.Add('------');
    end;
end;
}}}

*OR (Enumerator)*

{{{
var
  X: ISuperObject;
  AMember,
  OMember: IMember;
begin
  X := TSuperObject.Create(JSN);

  for AMember in X.A['adresses'] do
  begin
      for OMember in AMember.AsObject do
          Memo1.Lines.Add(OMember.Name + ' = ' + OMember.ToString);

      Memo1.Lines.Add('------');
  end;
}}}

*Output*
{{{
adress = blabla
city = Antalya
pc = 7160
------
adress = blabla
city = Adana
pc = 1170
}}}
------

他举了一个例子,但这是一个倍数。我有一条线。我该怎么办?

2 个答案:

答案 0 :(得分:0)

如果您的JSon是多级的,则迭代您要检索的级别,这里是'Rows'

ROW1= '{Rows : [{ scan : 12, find : 6, fImg : 2 }]}';

var
  X: ISuperObject;
  AMember,
  OMember: IMember;
begin
  X := TSuperObject.Create(ROW1);
  for AMember in X.A['Rows'] do
  begin
      for OMember in AMember.AsObject do
          Memo1.Lines.Add(OMember.Name + ' = ' + OMember.ToString);
      Memo1.Lines.Add('------');
  end;
end;

对于一个简单的对象,你只需迭代然后对象本身

ROW2= '{ scan : 12, find : 6, fImg : 2 }';

var
  X: ISuperObject;
  AMember,
  OMember: IMember;
begin
  X := TSuperObject.Create(ROW2);
  for OMember in X do
     Memo1.Lines.Add(OMember.Name + ' = ' + OMember.ToString);
end;

答案 1 :(得分:0)

procedure TForm4.Button1Click(Sender: TObject);
var
  json: ISuperObject;
begin
  json := SO('{"Rows":[{"scan":12,"find":6,"fImg":2}]}');

  ShowMessage(json.A['Rows'].O[0].I['scan'].ToString());
end;