Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Reading JSON from internet API


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Jan 27, 2018 12:13 PM Last Post By: Antonio Estevez Threads: [ Previous | Next ]
Grigori Mystwi

Posts: 2
Registered: 2/27/18
Reading JSON from internet API  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 27, 2018 5:33 AM
I need to read JSON from internet API (https://my-json-server.typicode.com/kgbzoma/TestJsonFile/all). I know how read it as String (Label1->Text=NetHTTPRequest1->Execute()->ContentAsString();)
Should i somehow now convert that string (if yes then how?) to get proper values or i should download it somehow else and then convert it (if yes can you say how)? I'm using C++ builder and FMX.
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: Reading JSON from internet API [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 27, 2018 12:13 PM   in response to: Grigori Mystwi in response to: Grigori Mystwi
El 27/01/2018 a las 20:14, Grigori Mystwi escribió:
I need to read JSON from internet API (https://my-json-server.typicode.com/kgbzoma/TestJsonFile/all). I know how read it as String (Label1->Text=NetHTTPRequest1->Execute()->ContentAsString();)
Should i somehow now convert that string (if yes then how?) to get proper values or i should download it somehow else and then convert it (if yes can you say how)? I'm using C++ builder and FMX.

Take a look to the JSON frameworks from the RTL:
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/JSON

By example:

...
#include <System.JSON.hpp>
...
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    String response= NetHTTPRequest1->Execute()->ContentAsString();
 
    if (TJSONArray *array= dynamic_cast<TJSONArray*>(TJSONObject::ParseJSONValue(BytesOf(response), 0)))
    try
    {
       if (TJSONObject *obj= dynamic_cast<TJSONObject*>(array->Items[0]))
       {
          TStringList *roles= new TStringList();
          try
          {
             String email= obj->Values[L"Email"]->Value();
             String phone= obj->Values[L"Numberphone"]->Value();
             bool active= StrToBool(obj->Values[L"Active"]->Value());
 
             if (TJSONArray *list= dynamic_cast<TJSONArray*>(obj->Values[L"Roles"]))
             {
                for (int i= 0; i< list->Count; ++i)
                   roles->Add(list->Items[i]->Value());
             }
 
             // Use email, phone active and roles as needed
 
          }
          __finally
          {
             delete roles;
          }
       }
    }
    __finally
    {
       delete array;
    }
}
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02