Topic: Need help with retrieving JSON Data - TJSONObject not working for me

Hello I'm trying to pair and retrieve JSON data but TJSONObject  is not working for me the the data is a valid JSON (RFC 8259) but no matter what I do is not showing anything for me. I think i'm doing something wrong. Please Help..

JSON DATA

[
   {
      "id":"1001",
      "email":"jdenero@testemail.com",
      "name":"John Denero",
      "memberships":[
         {
            "group":"Regular Users",
            "hourlyRate":{
               "amount":15,
               "currency":"USD"
            },
            "membershipStatus":"ACTIVE"
         }
      ]
   },
   {
      "id":"1002",
      "email":"mwilson@testemail.com",
      "name":"Mark Wilson",
      "memberships":[
         {
            "group":"Admin Users",
            "hourlyRate":{
               "amount":30,
               "currency":"USD"
            },
            "membershipStatus":"ACTIVE"
         }
      ]
   },
   {
      "id":"1003",
      "email":"psalmon@testemail.com",
      "name":"Peter Salmon",
      "memberships":[
         {
            "group":"Regular Users",
            "hourlyRate":{
               "amount":17,
               "currency":"USD"
            },
            "membershipStatus":"DEACTIVATED"
         }
      ]
   }
]

Code I'm Using. See attach project.

procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var
sl:TStringList;
jDataStream, jParent_01,jParent_02: TJSONObject;
jValue_01, jValue_02, jValue_03, jValue_04: TJSONString;
jArray: TJSONArray;
i: int;
begin
      sl:=TStringList.Create;
      sl.LoadFromFile(ExtractFilePath(Application.ExeName)+'\json.txt');
      jDataStream := nil;

      jDataStream:=TJSONObject(TJSONObject.ParseJSONValue(sl.Text));
      frm_main.Memo1.Text:=sl.text;
      frm_main.Memo2.Text:=''; //clear

      if jDataStream <> nil then                               
       begin

           frm_main.Memo2.Lines.add('json size: '+ IntToStr(jDataStream.size));     //size of json

          //Method 1.. get information from array
          for i := 0 to jDataStream.Size-1 do
             begin
                  jValue_01 := TJSONString(TJSONObject(jArray.Get(i)).GetPairByName('id').JsonValue);     //get pair
                  frm_main.Memo2.Lines.add(jValue_01.value);       //writes value
             end;

         frm_main.Memo2.Lines.Add('--------------');      //writes value
                  
         //Method 2... pair by name
         jValue_02 := TJSONString(jDataStream.GetPairByName('email').JsonValue);         //pair
         frm_main.Memo2.Lines.Add(jValue_02.Value);      //writes value

         //method 3.. pair by index
         jValue_03 := TJSONString(jDataStream.GetPairByIndex(0).JsonValue);           //Level 1
         frm_main.Memo2.Lines.Add(jValue_03.Value);      //writes value

        //method 4.. pair by index and then get information by name
         jParent_01 := TJSONObject(jDataStream.GetPairByIndex(1).JsonValue);           //Level 1
         jValue_03 := TJSONString(jParent_01.GetPairByName('name').JsonValue);         //pair
         frm_main.Memo2.Lines.Add(jValue_03.Value);      //writes value




     end;

      //free object
      sl.free
end;


begin

end.
Post's attachments

Attachment icon json.zip 337.26 kb, 284 downloads since 2020-06-01 

Re: Need help with retrieving JSON Data - TJSONObject not working for me

procedure DUAll;
var
JSONObject, JSONObjProp: TJSONObject;
JSONString: TJSONString;
JsonArray, JsonArrayCoord: TJSONArray;
i:integer;
begin
  try
    if http.Status() = 200 then
      begin
        JSONObject:=TJSONObject(TJSONObject.ParseJSONValue(http.responsetext));
          if JSONObject <> nil then begin
            JSONString:=TJSONString(JSONObject.GetPairByName('success').JsonValue);
                if Pos('not user', JSONString.Value) > 0  then begin
                    JSONString:=TJSONString(JSONObject.GetPairByName('days').JsonValue);
                    frmAdmin.bDays.Caption:=langIniG.ReadString('Admin','bDays','Осталось дней')+' :  '+JSONString.Value;
                    timerAu.Enabled:=False;
              end else
                  if Pos('1', JSONString.Value) > 0  then begin
                    JSONString:=TJSONString(JSONObject.GetPairByName('days').JsonValue);
                    frmAdmin.bDays.Caption:=langIniG.ReadString('Admin','bDays','Осталось дней')+' :  '+JSONString.Value;
                    JsonArray:=TJSONArray(JSONObject.GetPairByName('products').JsonValue);
                    frmAdmin.tgUserApp.ClearRows;
                     for i := 0 to JsonArray.Size-1  do begin
                        frmAdmin.tgUserApp.AddRow(1);
                        JSONObjProp := TJSONObject( TJSONObject(JsonArray.Get(i)));
                        JSONString := TJSONString(JSONObjProp.GetPairByName('id').JsonValue);
                        frmAdmin.tgUserApp.Cells[0,i] := JSONString.Value;
                        JSONString := TJSONString(JSONObjProp.GetPairByName('login').JsonValue);
                        frmAdmin.tgUserApp.Cells[1,i] := JSONString.Value;
                        JSONString := TJSONString(JSONObjProp.GetPairByName('password').JsonValue);
                        frmAdmin.tgUserApp.Cells[2,i] := JSONString.Value;
                        JSONString := TJSONString(JSONObjProp.GetPairByName('firstname').JsonValue);
                        frmAdmin.tgUserApp.Cells[3,i] := JSONString.Value;
                        JSONString := TJSONString(JSONObjProp.GetPairByName('lastname').JsonValue);
                        frmAdmin.tgUserApp.Cells[4,i] := JSONString.Value;
                      end;
                    timerAu.Enabled:=False;
                 end;
               end;
            end;
       except
    end;
end;

{
  "success": "1",
  "days": "0",
  "products": [
    {
      "id": "1",
      "login": "wer",
      "password": "wer",
      "firstname": "wer",
      "lastname": "er",
      "user_id": "2"
    },
    {
      "id": "2",
      "login": "dbde",
      "password": "m!",
      "firstname": "dghe",
      "lastname": "he",
      "user_id": "2"
    }
  ]
}

Re: Need help with retrieving JSON Data - TJSONObject not working for me

thank you sibprogsistem, I think the problem I'm having is that I need to read the array of JSON string without a leading pair therefore i'm not able to use the following code.


JsonArray:=TJSONArray(JSONObject.GetPairByName('products').JsonValue);


[
   {
      "id":"1001",
      "email":"jdenero@testemail.com",
      "name":"John Denero",
      "memberships":[
         {
            "group":"Regular Users",
            "hourlyRate":{
               "amount":15,
               "currency":"USD"
            },
            "membershipStatus":"ACTIVE"
         }
      ]
   },
  ....
]

Re: Need help with retrieving JSON Data - TJSONObject not working for me

you have an array of data and you are trying to load an object

{object}
[array]

5 (edited by livexox 2020-06-02 23:12:32)

Re: Need help with retrieving JSON Data - TJSONObject not working for me

Yes, the questions is how to retrieve the data from the following JSON String: All the samples I've seen in this forum do not work for the sample below.

[
   {
      "id":"1001",
      "email":"jdenero@testemail.com",
      "name":"John Denero",
      "memberships":[
         {
            "group":"Regular Users",
            "hourlyRate":{
               "amount":15,
               "currency":"USD"
            },
            "membershipStatus":"ACTIVE"
         }
      ]
   },
   {
      "id":"1002",
      "email":"mwilson@testemail.com",
      "name":"Mark Wilson",
      "memberships":[
         {
            "group":"Admin Users",
            "hourlyRate":{
               "amount":30,
               "currency":"USD"
            },
            "membershipStatus":"ACTIVE"
         }
      ]
   },
   {
      "id":"1003",
      "email":"psalmon@testemail.com",
      "name":"Peter Salmon",
      "memberships":[
         {
            "group":"Regular Users",
            "hourlyRate":{
               "amount":17,
               "currency":"USD"
            },
            "membershipStatus":"DEACTIVATED"
         }
      ]
   }
]

6 (edited by sibprogsistem 2020-06-03 06:53:10)

Re: Need help with retrieving JSON Data - TJSONObject not working for me

procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
var
sl:TStringList;
jDataStream, jParent_01,jParent_02: TJSONObject;
jValue_01, jValue_02, jValue_03, jValue_04: TJSONString;
jArray: TJSONArray;
i: int;
begin
      sl:=TStringList.Create;
      sl.LoadFromFile(ExtractFilePath(Application.ExeName)+'\json.txt');
      jDataStream := nil;

      jArray:=TJSONArray(TJSONObject.ParseJSONValue(sl.Text));
      frm_main.Memo1.Text:=sl.text;
      frm_main.Memo2.Text:=''; //clear
      if jArray <> nil then
       begin

           frm_main.Memo2.Lines.add('json size: '+ IntToStr(jArray.size));     //size of json

          //Method 1.. get information from array
          for i := 0 to jArray.Size-1 do
             begin
                  jValue_01 := TJSONString(TJSONObject(jArray.Get(i)).GetPairByName('id').JsonValue);     //get pair
                  frm_main.Memo2.Lines.add(jValue_01.value);       //writes value
             end;

     end;

      //free object
      sl.free
end;


begin

end.

jArray:=TJSONArray(TJSONObject.ParseJSONValue(sl.Text));

Post's attachments

Attachment icon json.7z 3.3 kb, 334 downloads since 2020-06-03 

Re: Need help with retrieving JSON Data - TJSONObject not working for me

Thank you so much sibprogsistem it works