Jump to content
bilcan

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;

 

 

 

Share this post


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.

Share this post


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; 

 

 

Share this post


Link to post
Share on other sites

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

×