#1 2017-12-12 20:52:10

ianc
Member
Registered: 2017-01-12
Posts: 17

Parsing JSON

Hi All

I have SOA project working as expected. A call to the interface
returns the following.

{"rows":[{"ID":1,"Name":"BE10065","iData":{"name":"BE10065","D1":"D1","D2":"D2","D3":"D3","D4":"D4","D5":"D5","D6":"D6","D7":"D7","D8":"D8","D9":"D9","D10":"D10","D11":"D11","D12":"D12"}}]}

What function on the client side can parse the data in the variant array "iData"

Im guessing i should be using JsonGet(ObjColumn,'Obj1.Obj2.Prop') for this

I just cant work out how to use this on the client side. Any help will be appreciated

Offline

#2 2017-12-12 22:32:03

igors233
Member
Registered: 2012-09-10
Posts: 234

Re: Parsing JSON

Easiest would be to use TDocVariantData and TDocVariant Variant, something like:

var
  v: Variant;
  rows, iData: TDocVariantData;
  i: Integer;
begin
  v := _JsonFast('{"rows":[{"ID":1,"Name":"BE10065","iData":{"name":"BE10065","D1":"D1","D2":"D2","D3":"D3","D4":"D4","D5":"D5","D6":"D6","D7":"D7","D8":"D8","D9":"D9","D10":"D10","D11":"D11","D12":"D12"}}]}');
  rows := _Safe(v.rows)^;
  for i := 0 to rows.Count - 1 do
  begin
    iData := _Safe(rows.Values[i].iData)^;
    if iData.Count > 0 then
      ShowMessage(iData.U['name'] + '; ' + iData.U['D1'] + '; ' + iData.U['D2'] + '; ');
 end;
end;

Offline

#3 2017-12-12 23:40:36

ianc
Member
Registered: 2017-01-12
Posts: 17

Re: Parsing JSON

Thanks Igors233

Thats exactly what I was after. Much appreciated will implement

Offline

#4 2020-07-28 09:27:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: Parsing JSON

Small tip: use PDocVariantData instead of TDocVariantData.

var rows, iData: PDocVariantData;
...
rows := _Safe(v.rows);
...
iData := _Safe(...);

Using pointers would make the code faster and safer, e.g. if you modify the data.

Offline

Board footer

Powered by FluxBB