How to create JSON-file in Delphi using SuperObject lib?
Asked Answered
A

4

12

I'm using Delphi 2010 and superobject library.

I have understand how to parse json-file, but I have no ideas how to create json?

The algorithm is:

  1. Parsing JSON and load in TStringGrid
  2. Adding data
  3. Save all TStringGrid data to json.

Need some example.

Thanks.

Agony answered 28/5, 2013 at 10:41 Comment(6)
So, you don't know how to save data into JSON string or how to save delphi string to file ?Hypochromia
I don't know how to save data into jsonAgony
superobject.googlecode.com/git/readme.html Try to dump you data into array of variant and then use SA helper to create arrayHypochromia
I have read this. but I would like to know how to save difficult structures like in initial JSON in tutorial you gave me )Agony
see the line obj := ctx.AsJson<TData>(data); - look into sources (or into debugger) how this is actually implemented.Hypochromia
Look into demo code.google.com/p/superobject/source/browse/tests/…Hypochromia
K
26

Code sample to feed following structure to JSON object, then save to file:

(*
{
  "name": "Henri Gourvest", /* this is a comment */
  "vip": true,
  "telephones": ["000000000", "111111111111"],
  "age": 33,
  "size": 1.83,
  "addresses": [
    {
      "address": "blabla",
      "city": "Metz",
      "pc": 57000
    },
    {
      "address": "blabla",
      "city": "Nantes",
      "pc": 44000
    }
  ]
}
*)

procedure SaveJson;
var
  json, json_sub: ISuperObject;
begin
  json := SO;

  json.S['name'] := 'Henri Gourvest';
  json.B['vip'] := TRUE;
  json.O['telephones'] := SA([]);
  json.A['telephones'].S[0] := '000000000';
  json.A['telephones'].S[1] := '111111111111';
  json.I['age'] := 33;
  json.D['size'] := 1.83;

  json.O['addresses'] := SA([]);

  json_sub := SO;
  json_sub.S['address'] := 'blabla';
  json_sub.S['city'] := 'Metz';
  json_sub.I['pc'] := 57000;
  json.A['addresses'].Add(json_sub);

  json_sub.S['address'] := 'blabla';
  json_sub.S['city'] := 'Nantes';
  json_sub.I['pc'] := 44000;
  json.A['addresses'].Add(json_sub);

  json.SaveTo('C:\json_out.txt');

  json := nil;
  json_sub := nil;
end;
Kalmia answered 28/5, 2013 at 11:13 Comment(2)
Perhaps json.A['telephones'].S[0] := '000000000'; can be changed to json['telephones[0]'] := '000000000'; and json.A['addresses'].Add(json_sub);` with json['addresses[]'] := json_sub;Hypochromia
Could you please add code to show how to add named object array? such as under address you will have building object that has an address?Aphaeresis
D
8

Reading help file: https://github.com/hgourvest/superobject/blob/master/README.md

And then reading sources for TSuperArray ('Use the source, Luke')

Results in the following snippet:

var
  obj: ISuperObject;
  a:   TSuperArray; // shortcut
begin
  obj := TSuperObject.Create(stArray);
  // or obj := SA([]);

  a := obj.AsArray;

  a.s[0] := 'aaaa';
  a.s[1] := 'bbbb';
  a.s[3] := 'cccc';

  ...

  obj.SaveTo('File.txt');
  a := nil; obj := nil;

  ...
end;

There is also the quote from help file: obj['foo[]'] := value; // add an item array
This suggests another way to populate an array (if the root object itself is not an array). Quoting http://code.google.com/p/superobject/source/browse/tests/test_usage.dpr

my_array := TSuperObject.Create(stArray);
    my_array.I[''] := 1; // append
    my_array.I[''] := 2; // append
    my_array.I[''] := 3; // append
    my_array.I['4'] := 5;

And later this object-array is inserted as property into yet another object

    my_object := TSuperObject.Create(stObject);
    my_object.I['abc'] := 12;
   // my_object.S['path.to.foo[5]'] := 'bar';
    my_object.B['bool0'] := false;
    my_object.B['bool1'] := true;
    my_object.S['baz'] := 'bang';
    my_object.S['baz'] := 'fark';
    my_object.AsObject.Delete('baz');
    my_object['arr'] := my_array;
Dayledaylight answered 28/5, 2013 at 10:59 Comment(0)
R
0

Example with TObjectList

type
  TPeople = class
    ID: Integer;
    Name: string;
    Address: string;
    function JSON: ISuperObject;
  end;

  TMyClass = class
    TList: TObjectList<TPeople>;
    procedure SaveToJSON(var JSON: ISuperObject);
  end;


function TPeople.JSON: ISuperObject;
begin
  Result := SO;  //defining a JSON Object
  Result.I['id'] := ID;
  Result.S['name'] := Name;
  Result.S['address'] := Address;
end;

procedure TMyClass.SaveToJSON(var JSON: ISuperObject);
var
  Obj: TDLSBatchField;
begin
  JSON := SO;
  JSON.I['count'] := FItems.Count;
  JSON['items'] := SA([]);  //defining an array
  for Obj in FItems do
    JSON.A['items'].Add(Obj.JSON);
end;
Recommendation answered 31/1, 2023 at 15:30 Comment(0)
J
-1

A very good library for that is the LkJson: http://sourceforge.net/projects/lkjson/

To parse:

var jsText : String; jsObj : TlkJSONobject; begin

jsObj:=TlkJSON.ParseText(jsText) as TlkJSONobject;

To convert it back into text:

jsText := TlkJSON.GenerateText(jsObj);
Jurisdiction answered 30/5, 2013 at 19:32 Comment(1)
OP specifically is using a particular library already called SuperObject, this answer has nothing to do with it.Anam

© 2022 - 2024 — McMap. All rights reserved.