Jump to content

Reading JSON data


Recommended Posts

What is the way of reading that kind of JSON data. I tried some ways which I found at the forum but I could not. 

 

{This is Embarcadero example of Json}

uses System.json;
 
var
   Obj, ObjCons, ObjIpp: TJSONObject;
 
begin
   Obj := TJSONObject.Create;
try
  ObjIpp := TJSONObject.Create;
  ObjIpp.AddPair('ipp', TJSONNumber.Create(1122368));
 
  ObjCons := TJSONObject.Create;
  ObjCons.AddPair('decision', 'NA');
  ObjCons.AddPair('idPatient', ObjIpp);
  ObjCons.AddPair('idStructure', TJSONNumber.Create(300000000023887));
 
  Obj.AddPair('consentement', ObjCons);
  Memo1.Lines.Add(Obj.ToJSON);
finally
  Obj.Free;
end;
 
end;

 

 

 

Link to post
Share on other sites

JSON stands for JavaScript Object Notation, which means that every JavaScript object can already written in this notation directly. By using the ECMA API unit ECMA.Json you can simply "stringify" every object.

 

Here's a brief example:

uses
  ECMA.Json;

// specify the object
var
  Obj := class
    consentement = class
      decision: String = 'NA';
      idPatient = class
        ipp: Integer = 1122368;
      end;
      idStructure: Integer = 300000000023887;
    end;
  end;

// convert the object to a string
JSON.Stringify(Obj);

This might not look like the Pascal you are used to, but its a good compromise between Object Pascal and JavaScript.

 

If you prefer you could also write types for the classes, instantiate and fill the objects. While that would look more like the Pascal you might know, it's a bit bloated and only useful if you are using the classes anyway for the business logic.

Link to post
Share on other sites

JS and SMS have a built in way of handling JSON, it's auto transfered to object (some variant variable) when you call JSON.parse function;

 

Take a look at LocalStorage demo within SMS installation ("SMS INstallation Dir\Projects\A Smart Book Demos\LocalStorage\LocalStorage.sproj"). Look for GetData and SetData methods; 

 

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...