Recent

Author Topic: JSON how do I extract this  (Read 5888 times)

vonskie

  • Full Member
  • ***
  • Posts: 184
JSON how do I extract this
« on: August 21, 2017, 03:51:18 am »
Here is a sample dataset

"timestamps_int": {
        "23": [
            20170820235522,
            20170820235022,
            20170820234522,
            20170820234022,
            20170820233522,
            20170820233022,
            20170820232522,
            20170820232022,
            20170820231522,
            20170820231022,
            20170820230522,
            20170820230022
        ],
        "22": [
            20170820225522,
            20170820225022,
            20170820224522,
            20170820224022,
            20170820223522,
            20170820223022,
            20170820222522,
            20170820222022,
            20170820221522,
            20170820221022,
            20170820220522,
            20170820220022
        ],
        "21": [
            20170820215522,
            20170820215022,
            20170820214522,
            20170820214022,
            20170820213522,
            20170820213022,
            20170820212522,
            20170820212022,
            20170820211522,
            20170820211022,
            20170820210522,
            20170820210022
        ],

If I use the following code

 // create from string
   jData := GetJSON(memo1.text);

   // output as a flat string
   s := jData.AsJSON;

   //  output as nicely formatted JSON
   s := jData.FormatJSON;

   // cast as TJSONObject to make access easier
   jObject := TJSONObject(jData);
                                 
 s := jData.FindPath('timestamps_int.23[1]').AsString;
   showmessage(s);

This will get me what I want if I know the hour I want is 23...

What I want to get though is to tell it something like this..

 s := jData.FindPath('timestamps_int.[1][1]').AsString;

Instead of telling it 23[1] I want to say grab the first hour in the object and grab the first element from that array.. Does that make sense? Is there a way to do that?










molly

  • Hero Member
  • *****
  • Posts: 2330
Re: JSON how do I extract this
« Reply #1 on: August 21, 2017, 05:08:37 am »
jArray := TJSONArray(jData.FindPath('timestamps_int'));

and iterate the items, taking count into consideration.

vonskie

  • Full Member
  • ***
  • Posts: 184
Re: JSON how do I extract this
« Reply #2 on: August 21, 2017, 05:29:31 am »
Thank you! Thank You!

 jArray := TJSONArray(jData.FindPath('timestamps_int'));

 showmessage(jarray.Arrays[0].Strings[0]);

Exactly what I was looking for

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: JSON how do I extract this
« Reply #3 on: August 21, 2017, 05:37:18 am »
ah, yes.... that is also possible. I haven't even thought of that  :D

just because we can (do it differently):
Code: Pascal  [Select][+][-]
  1. procedure ProcessJSON(JSONDataString:String);
  2. var
  3.   jData:TJSONData;
  4.   jArray:TJSonArray;
  5.   i,j:Integer;
  6. begin
  7.   jData:=GetJSON(JSONDataString);
  8.   jArray:=TJSONArray(jData.FindPath('timestamps_int'));
  9.   for i:=0 to Pred(jArray.Count) do
  10.   begin
  11.     WriteLn;
  12.     Writeln('Number of items in this array = ',jArray.Items[i].Count);
  13.     for j:=0 to Pred(jArray.Items[i].Count) do
  14.     begin
  15.       WriteLn('Item[',i,'].[',j:2,'] = ',jArray.Items[i].Items[j].AsString);
  16.     end;
  17.   end;
  18.   WriteLn;
  19.   jData.Free;
  20. end;
  21.  

But, yours is as just as good (that is if it works for you)  :)

vonskie

  • Full Member
  • ***
  • Posts: 184
Re: JSON how do I extract this
« Reply #4 on: August 21, 2017, 03:56:30 pm »
Thanks again!




 

TinyPortal © 2005-2018