Recent

Author Topic: Access JSON data  (Read 6778 times)

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Access JSON data
« on: May 25, 2018, 12:37:56 pm »
My app have to manag with many JSON requests.
A simple JSON object can be accessed so easily;

Code: Pascal  [Select][+][-]
  1. procedure GetJson(Sender: TObject);
  2. var
  3.   resultado: String;
  4.   Data: TJSONData;
  5.   id,timestamp,data1,data2,data3,data4,data5: String;
  6. begin
  7. Resultado := (Read_URL_GET('https://myweb.com'));
  8. if resultado = 'ERROR' then // Read_URL_GET function returns 'ERROR' if something fails.
  9.    begin
  10.    exit;
  11.    end;
  12. // Example resultado: ["11","1527214375","hello","goodbye","flowers","car","food"]
  13. data := getjson(resultado);
  14. id := TJSONBoolean(Data.Items[0]).Value;
  15. timestamp := TJSONBoolean(Data.Items[1]).Value;
  16. data1:= TJSONBoolean(Data.Items[2]).Value;
  17. data2:= TJSONBoolean(Data.Items[3]).Value;
  18. data3:= TJSONBoolean(Data.Items[4]).Value;
  19. data4:= TJSONBoolean(Data.Items[5]).Value;
  20. data5:= TJSONBoolean(Data.Items[6]).Value;
  21. end;

This is working perfect.

But now i have to manage with this JSON format: (example)

Code: Pascal  [Select][+][-]
  1. [{"name":"timestamp","value":"1527214375"},{"name":"food","value":"meat"},{"name":"drink","value":"soda"},{"name":"job","value":"unocuped"},{"name":"married","value":"0"}]

How i can access the data?
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Access JSON data
« Reply #1 on: May 25, 2018, 02:19:10 pm »
  • Cast result of GetJSON to TJSONArray
  • Access each record through its Objects property
  • Access each record's field through their data type property
Documentation is your friend: https://www.freepascal.org/docs-html/fcl/fpjson/index.html
« Last Edit: May 26, 2018, 03:54:40 pm by Leledumbo »

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Access JSON data
« Reply #2 on: May 25, 2018, 10:04:27 pm »
Code: Pascal  [Select][+][-]
  1. Procedure extractJSONdata(); // JUST FOR TEST
  2. var
  3. Data: TJSONData;  
  4. DataArrayItem: TJSONData;
  5.  I: Integer;
  6. name,namevalue: String;
  7. begin
  8. $datos = [{"name":"timestamp","value":"1527214375"},{"name":"food","value":"meat"},{"name":"drink","value":"soda"},{"name":"job","value":"unocuped"},{"name":"married","value":"0"}];
  9. Data := GetJSON(Datos);
  10. for I := 1 to Data.Count - 1 do // i want skip first pair of data = {"name":"timestamp","value":"1527214375"}
  11. begin
  12. DataArrayItem := TJSONObject(Data.Items[I]);
  13. name :=  TJSONObject(DataArrayItem.Items[0]).Items[0].value;
  14. namevalue :=  TJSONObject(DataArrayItem.Items[0]).Items[1].value;
  15. memo1.lines.add(name+','+namevalue);
  16.  
  17. end;  
  18. end;

I want this lines:
food,meat
drink,soda
job,unocuped
married,0

but I got a SIGSEGV error...
Where is the fail?
« Last Edit: May 25, 2018, 10:13:14 pm by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Access JSON data
« Reply #3 on: May 26, 2018, 04:04:13 pm »
Where is the fail?
I usually leave those who didn't do what I told in previous reply, but let this once an exception. Next time, if you don't fix that attitude, I won't help anymore.
Code: Pascal  [Select][+][-]
  1. {$H+}
  2. uses
  3.   jsonparser,fpjson;
  4. var
  5.   datos: string;
  6.   Data: TJSONArray;  
  7.   DataArrayItem: TJSONObject;
  8.   I: Integer;
  9.   name,namevalue: String;
  10. begin
  11.   datos := '[{"name":"timestamp","value":"1527214375"},{"name":"food","value":"meat"},{"name":"drink","value":"soda"},{"name":"job","value":"unocuped"},{"name":"married","value":"0"}]';
  12.   Data := TJSONArray(GetJSON(Datos));
  13.   for I := 1 to Data.Count - 1 do // i want skip first pair of data = {"name":"timestamp","value":"1527214375"}
  14.   begin
  15.     DataArrayItem := Data.Objects[i];
  16.     name :=  DataArrayItem['name'].AsString;
  17.     namevalue :=  DataArrayItem['value'].AsString;
  18.     writeln(name+','+namevalue);
  19.   end;
  20. end.
  21.  

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Access JSON data
« Reply #4 on: May 26, 2018, 04:59:47 pm »
I usually leave those who didn't do what I told in previous reply, but let this once an exception. Next time, if you don't fix that attitude, I won't help anymore.
Yes you will... (You are allowed to borrow "grumpy" mode...)
It can be very frustrating to reduce somebody's real problem to very simple code and subsequently ignored for a serious answer because the code looks too easy a solution. That's what happened here.
Been there, done that, still do it. You will do it too.
It is the difference between true working brains and grey matter.... ::) O:-) :D

You can also over-complicate your answer on purpose, because then they believe you... :) :) :D :D ;D (In this particular case: add line numbers... :o or an audio explanation :'( not video of course, that will confuse them)
« Last Edit: May 26, 2018, 05:10:30 pm by Thaddy »
Specialize a type, not a var.

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Access JSON data
« Reply #5 on: May 27, 2018, 02:24:14 am »
Quote
I usually leave those who didn't do what I told in previous reply, but let this once an exception. Next time, if you don't fix that attitude, I won't help anymore.

I just entered here after i found the solution by myself. Not strictly the same solution, but one that worked i think in the same way:
Code: Pascal  [Select][+][-]
  1.     name :=  DataArrayItem.Items[0].AsUnicodeString;
  2.     namevalue :=  DataArrayItem.Items[1].AsUnicodeString;

Thanks for your help. Your link in your first reply was very usefull. Your last reply was usefull too, even when not very friendly. But im here to learn and share learnings, not to make friends, so thank you again.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

 

TinyPortal © 2005-2018