Jump to content


Photo

Reading JSON data

JSON Delphi

  • Please log in to reply
3 replies to this topic

#1 B. SEZER

B. SEZER
  • Members
  • 15 posts
  • LocationTurkey

Posted 17 February 2016 - 01:51 PM

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;

 

 

 


  • Jørn E. Angeltveit likes this

#2 Christian-W. Budde

Christian-W. Budde
  • Members
  • 334 posts
  • LocationGermany

Posted 17 February 2016 - 03:10 PM

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.
  • Jørn E. Angeltveit likes this

#3 Igor Savkic

Igor Savkic
  • Members
  • 185 posts

Posted 17 February 2016 - 03:13 PM

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; 

 

 


  • Jørn E. Angeltveit likes this

#4 B. SEZER

B. SEZER
  • Members
  • 15 posts
  • LocationTurkey

Posted 18 February 2016 - 04:16 PM

Thank you, I solved the problem.







Also tagged with one or more of these keywords: JSON, Delphi

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users