适用于Firemonkey的Json解析对象XsuperObject使用方法介绍

XSuperObject是适用于FileMonkey平台的JSON解析组件,能够在Android,IOS,MACOS,WINDOWS等多个平台使用

点击下载 

 

Sample JSON

{
  "name":"Onur YILDIZ", 
  "vip":true,
  "telephones":["000000000","111111111111"],
  "age":24,
  "size":1.72,
  "adresses":[
    {
      "adress":"blabla",
      "city":"Antalya",
      "pc":7160
    },
    {
      "adress":"blabla",
      "city":"Adana",
      "pc":1170
    }
  ]
}

 

Delphi Code

var
  X:ISuperObject;
begin
  X :=TSuperObject.Create({});
  X.S[name]:=Onur YILDIZ;
  X.B[vip]:=true;
  with X.A[telephones]do
  begin
   Add(000000000);
   Add(111111111111);
  end;
  X.I[age]:=24;
  X.F[size]:=1.72;
  with X.A[adresses].O[0]{AutoCreate}do
  begin
    S[adress]:=blabla;
    S[city]:=Antalya;
    I[pc]:=7160;
  end;
  // or
  X.A[adresses].O[1].S[adress]:=blabla;
  X.A[adresses].O[1].S[city]:=Adana;
  X.A[adresses].O[1].I[pc]:=1170;

 

Super Expression

const
  JSON ={ "o": { +
             "1234567890": {+
             "last use date": "2010-10-17T01:23:20",+
             "create date": "2010-10-17T01:23:20",+
             "name": "iPhone 8s"+
                 }+
           },+
           "Index": 0, +
           "Data": {"Index2": 1}, +
           "a": [{+
             "last use date": "2010-10-17T01:23:20",+
             "create date": "2010-11-17T01:23:20",+
             "name": "iPhone 8s",+
             "arr": [1,2,3] +
           }, +
           {+
             message: "hello"+
           }]+
         };

var
  X:ISuperObject;
  NewJSon:ISuperObject;
  NewArray:ISuperArray;
begin
  X := SO(JSON);
  ShowMessage( X[o."1234567890"."last use date"].AsString);
  ShowMessage( X[a[Index]."create date"].AsString);
  ShowMessage( X[a[Data.Index2].message].AsString);
  X[a[0].arr].AsArray.Add(test1);
  // -----
  NewJSON:= X[{a: a[Index], b: a[Data.Index2].message, c: o."1234567890".name, d: 4, e: a[0].arr[2], f: " :) "}].AsObject;
  NewArray:= X[[a[Index], a[Data.Index2].message, Data.Index2, Index, 1, "1", "test"]].AsArray;
end;

 

Variant

var 
  X:ISuperObject;
begin 
  X :=TSuperObject.Create;
  X.V[A]:=1;
  X.V[B]:=2;
  X.V[C]:=1.3;
  X.V[D]:=False;
  X.V[E]:=Null;
  Memo1.Lines.Add(X.AsJSON);
end;

 

Output

{"A":1,"B":"2","C":1.3,"D":false,"E":null}

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-1do
    begin
      Obj:= O[J];
      Obj.First;
      whilenotObj.EoFdo
      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);

  forAMemberin X.A[adresses]do
  begin
      forOMemberinAMember.AsObjectdo
          Memo1.Lines.Add(OMember.Name+ = +OMember.ToString);

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

 

Output

adress = blabla
city
=Antalya
pc
=7160
------
adress
= blabla
city
=Adana
pc
=1170

Marshalling

type

  TTestSet=(ttA, ttB, ttC);

  TTestSets=set of TTestSet;
 
  TSubRec= record
    A:Integer;
    B:String; 
  end;

  TSubObj=class
    A:Integer;
    B:Integer; 
  end;  
  
  TTest=class// Field, Property Support
  private
    FB:String;
    FSubObj:TSubObj;
    FSubRec:TSubRec;
    FTestSets:TTestSets;
  public
    A:Integer;
    B:TTestSet;
    C:Boolean;
    property D:String read FB write FB;
    property E:TSubRec read FSubRec write FSubRec;
    property F:TSubObj read FSubObj write FSubObj;
    property G:TTestSets read FTestSets write FTestSets;
  end;
  
  TTestRec= record // Only Field Support
    A:Integer;
    B:TTestSet;
    C:Boolean;
    D:String;
    E:TSubRec;
    F:TSubObj;
    G:TTestSets;
  end;
  
  implementation
  ...
  
  var 
    Parse:TTest;// For Class;
    S:String;
  begin
    Parse:=TTest.FromJSON({"A": 1, "B": 0, "C": true, "D": "Hello", "E":{"A": 3, "B": "Delphi"}, "F": {"A": 4, "B": 5}, "G": [0,2]});
    S :=Parse.AsJSON;
  end;
  
  
  ...
  var
    Parse:TTestRec;// For Record;
    S:String;
  begin
    Parse:=TSuperRecord<TTestRec>.FromJSON({"A": 1, "B": 0, "C": true, "D": "Hello", "E":{"A": 3, "B": "Delphi"}, "F": {"A": 4, "B": 5}, "G": [0,2]});  
    S :=TSuperRecord<TTestRec>.AsJSON(Parse);
  end;
  
  

 


 

适用于Firemonkey的Json解析对象XsuperObject使用方法介绍,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。