Jump to content
Magno

Help on reading values from a JSON array

Recommended Posts

Hi! Well I am struggling on this, maybe it's late and I am tired enough, I'd appreciate any help with. Below my function:
 

type
  TReadings = record
    Time: Integer;
    Flex: Boolean;
    Message: String;
end;  

function GetReadings: TReadings;
var
   JSonObject: TJSONObject;
   JSonValue: TJSonValue;
   JsonArray: TJSONArray;
   ArrayElement: TJSonValue;
   s: string;
begin
   Result.Message := '';
   Result.Time := 0;
   Result.Flex := false;
   // example of a json
   s := '{"A":0,"B":0,"C":1,"D":0,"K":{"T":"X","acc":81,"max":116},"result":[{"type":{"TP":30.3,"message":'+
        '"Added successfuly"},"time":503,"flex":"yes"}]}';

   JSonValue := TJSonObject.ParseJSONValue(s);
   JsonArray := JSonValue.GetValue<TJSONArray>('result');

   for ArrayElement in JsonArray do
    // how do I could read message, time and flex values?

   JSonValue.Free;
end;

I need to read that 3 values inside the array. So far I can move the array into the JsonArray but I don't know how to extract the values. Of course I could made a string parse and solve the issue, but I am will be glad if I can fix it using that Json classes to populate the result record.

 

Again, thanks for any help.

Share this post


Link to post

You has many levels in this JSON...

some like this...

 

uses
  System.JSON;

procedure TForm1.Button1Click(Sender: TObject);
var
  s : string;
  jv: TJSONValue;
  ja: TJSONArray;
  jo: TJSONObject;
  jp: TJSONPair;
begin
  s := '{"A":0,"B":0,"C":1,"D":0,' +      { }
    '"K":{"T":"X","acc":81,"max":116},' + { }
    '"result":[{"type":{"TP":30.3,"message":"Added successfuly"},"time":503,"flex":"yes"}]}';
  //
  try
    jo := TJSONValue.ParseJSONValue(s) as TJSONObject; // if JSON is good, go ahead. Else, break with an Exception!
    //
    try
      jp         := jo.Get('result'); // 'K', 'result' // direct to the point!
      Memo1.Text := jp.JsonValue.ToString;
      //
      if jp.JsonValue is TJSONObject then
        for var i: integer := 0 to TJSONObject(jp.JsonValue).Count - 1 do
          begin
            // Memo1.Lines.Add(TJSONObject(jp.JsonValue).Pairs[i].ToJSON)
            Memo1.Lines.Add(TJSONObject(jp.JsonValue).Pairs[i].JsonString.ToString + ' is ' + TJSONObject(jp.JsonValue).Pairs[i].JsonValue.ToString);
          end
      else // array
        begin
          for var A in TJSONArray(jp.JsonValue) do
            if A is TJSONObject then
              for var z: integer := 0 to (TJSONObject(A).Count - 1) do
                Memo1.Lines.Add(TJSONObject(A).Pairs[z].JsonString.ToString + ' is ' + TJSONObject(A).Pairs[z].JsonValue.ToString);

        end;
    except
      // PAIR not valid!!! Key not found...
    end;
    //
    //
    Memo1.Lines.Add('-----------------');
    //
    // walking on the road...
    for var O in jo do
      begin
        Memo1.Lines.Add('Object: ' + O.JsonString.ToString);
        //
        if O.JsonValue is TJSONArray then
          begin
            for var A in TJSONArray(O.JsonValue) do
              begin
                Memo1.Lines.Add('...Is an Array: ' + A.ToString);
                // ... recursive function here...
              end;
          end
        else // is an object... but... "if" is a array... needs a recursive too???
          begin
            Memo1.Lines.Add('...Not an Array is Object: ' + O.ToString);
          end;
      end;
  except
    // JSON not valid!!!
  end;
end;

image.thumb.png.6c97fb3f587bd99b1a1e350cb059fc05.png

 

Edited by programmerdelphi2k

Share this post


Link to post
TJSONObject(TJSONObject(TArrayElement).GetValue('type')).GetValue<String>(message, messagestring);
TJSONObject(TArrayElement).GetValue<Integer>('time', timeinteger);
TJSONObject(TArrayElement).GetValue<String>('flex', flexboolean);

 

Also, don't forget to wrap the inner code in a Try...Finally block so you free JSONValue no matter what.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×