Page 1 of 1

JSON in Delphi 7

PostPosted: January 29th, 2010, 5:53 am
by danielHP
Im using Delphi 7 and want to get JSON data from a memo object.
Its kinda look like this in the memo
[{"id":"1","curr_id":"1","name":"XXX","is_a":"1"},{"id":"2","curr_id":"2","name":"YYY","is_a":"9"},
{"id":"1","curr_id":"1","name":"ZZZ","is_a":"1"},{"id":"2","curr_id":"2","name":"AAA","is_a":"9"},
{"id":"1","curr_id":"1","name":QQQ","is_a":"1"}{"id":"2","curr_id":"2","name":CCC","is_a":"9"}]
the real data is more complex and hectic than above.
I want to separate each member into separate column in a table. Namely column id, curr_id, name,etc.
The JSON.org has pointed me to 3 different component
* Delphi Web Utils.
* JSON Delphi Library.(lkJSON)
* JSON Toolkit.(superobject v1.2.3)
and each one of them lacked (easy) documentation.
If anyone have already attempt to do something similar or know how to use the component, please help me.
Thank you.

Re: JSON in Delphi 7

PostPosted: January 30th, 2010, 3:54 pm
by Kambiz
I looked at those libraries. As you said they suffer from a lack of documentation.

JSON Toolkit.(superobject v1.2.3) seems to be the well designed one but I couldn't find out how to use it.
Delphi Web Utils is my next choice according to the way it has been coded, but no way to find how to use it.
JSON Delphi Library (lkJSON) is messy a bit but if you look at its sample2.dpr, it'll somehow help you to understand its usage.

Re: JSON in Delphi 7

PostPosted: March 16th, 2010, 4:42 pm
by surfzone
JSON Delphi Library (lkJSON)

Example of use:

I get this JSON string:

{"Geocod": [{"acuracy": 90.12, "Addr": {"dir": "Carrer Major,10", "cp": "08551", "pob": "Tona", "prov": "Barcelona", "pais": "Spain" },"Coor": { "lat": 2.233180, "lng": 41.853790 }}, {"acuracy": 80.21, "Addr": {"dir": "Carrer Major, 30", "cp": "08551", "pob": "Tona", "prov": "Barcelona", "pais": "Spain" },"Coor": { "lat": 2.333180, "lng": 41.253790 }}]}

This is on Memo2.Lines;

Delphi example code:

procedure TForm1.Button7Click(Sender: TObject);
var
js: TlkJSONobject;
ja: TlkJSONlist;
i: integer;
s: string;
begin
Memo3.Lines.Clear;

js := TlkJSON.ParseText(Memo2.Text) as TlkJSONobject;

ja := js.Field['Geocod'] as TlkJSONlist;

Memo3.Lines.Add(Format('Registers: %d', [ja.Count]));

for i := 0 to ja.Count - 1 do
begin
s := VarToStr(ja.Child[i].Field['acuracy'].Value) + ' - ' +
VarToStr(ja.Child[i].Field['Addr'].Field['dir'].Value) + ' - ' +
FormatFloat('0.0000', ja.Child[i].Field['Coor'].Field['lat'].Value) + ' - ' +
FormatFloat('0.0000', ja.Child[i].Field['Coor'].Field['lng'].Value);
Memo3.Lines.Add(s);
end;
end;