Json fragment (tech)
De Wiki1000
Un fragment JSon est un élément d'un document JSon qui peut être manipulé comme une variable.
Exemple :
//Procedure TestJson; var json:TJSon; va,v1,v2:variant; begin json := TJson.Create( '{"a":[{"e1":1,"e2":2,"e3":{"e31":"v31","e32":["e321","e322"]}}]}' ); va := json.a[0]; v1 := json.a[0].e1; v2 := va.e1; if v1<>v2 then showMessage('failed') else showMessage('Ok'); end;
Type de données
Type de donnée | Usage |
---|---|
variant | Tout fragment |
TjsonStruct | Structure |
TjsonArray | Tableau |
Opération sur un fragment
op1 | op2 | ope | rslt |
j1={a:0} | j2={b:0} | j1.assign(j2) | j1=={b:0} |
j1={a:0} | j2={b:0} | j1.merge(j2) | j1==[{a:0},{b:0}] |
j1={s:{a:0}} | j2={s:{b:0}} | j1.s.assign(j2.s) | j1=={s:{b:0}} |
j1={s:{a:0}} | j2={s:{b:0}} | j1.s.merge(j2.s) | j1=={s:{a:0,b:0}} |
j1={a:0} | j2={s:{b:0}} | j1.assign(j2.s) | j1=={b:0} |
j1={a:0} | j2={s:{b:0}} | j1.merge(j2.s) | j1=={a:0,b:0} |
AddStruct
var json1,json2:TJSon; vs,v1,v2:variant; begin json1 := TJson.Create('{}'); json1.AddStruct('b').c := 1; v1 := json1.b.c; if v1=1 then showMessage('ok') else showMessage('failed'); end;
//Procedure TestJson1; var json1,json2:TJson; begin json1 := Tjson.Create('{}'); json1.meta.id := 1; json2 := TJson.Create('{}'); json2.bp.name := 'name'; json2.bp.version := '1'; json2.bp.addStruct('data','{...}'); showMessage(json2.AsString); json1.meta.addStruct('bp',json2.bp); showMessage(json1.AsString); json1.CopyToClipboard; end;
Clear (tableau)
//Procedure TestJson4; var json:TJSon; va,v1:variant; begin json := TJson.Create('{}'); json.b[0].a[0].Values := '1'; va := json.b[0].a; va.Clear(0); if va.count=0 then showMessage('ok') else showMessage('failed'); end;
Assign
//Procedure TestJSon8; var json1,json2:TJSon; vb,v1:variant; begin json1 := TJson.Create('{}'); json1.a.b := 1; json1.a.AddStruct('b','{c:1}'); vb := json1.a.b; if not vb.Exists('c') then begin showMessage('failed'); Exit; end; v1 := vb.c; if (v1<>1) then begin showMessage('failed'); Exit; end; // replace content of b vb.Assign('{d:2}'); if vb.Exists('c') then begin showMessage('failed'); Exit; end; if not vb.Exists('d') then begin showMessage('failed'); Exit; end; v1 := vb.d; if (v1=2) then showMessage('ok') else showMessage('failed'); end;
Merge
//Procedure TestJson9; var json1,json2:TJSon; vs,v1,v2:variant; begin json1 := TJson.Create('{}'); json1.a.b.x := 1; json1.a.b.Merge('{c:1}'); v1 := json1.a.b.c; if v1=1 then showMessage('ok') else showMessage('failed'); end;
//Procedure TestJson9; var json1,json2:TJSon; vx,v1,v2:variant; begin json1 := TJson.Create('{}'); json1.a.b.c := 1; v1 := json1.a.b; if not v1.Exists('c') then begin showMessage('failed (1)'); Exit; end; json2 := TJson.Create('{}'); json2.a.b.d := 1; v2 := json2.a.b; if not v2.Exists('d') then begin showMessage('failed (2)'); Exit; end; v1.merge(v2); if not v1.Exists('d') then begin showMessage('failed (3)'); Exit; end; vx := json1.a.b.d; if vx=1 then showMessage('ok') else showMessage('failed (2)'); end;