FMX/Tip

FMX - JSON 정리

지병철 2018. 10. 30. 15:34

사용해야 할 use 절은 System.JSON, Sytem.JSON.Types, System.JSON.Writers, System.JSON.Builders;


1. JSON Writer

아래와 같은 JSON을 만들 때

{

"colors" : [

{

"name": "red",

"hex": "#f00"

}

]

}


1) Old framework

JSONColor := TJSONObject.Create;

JSONColor.AddPair('name', 'red');

JSONColor.AddPair('hex', '#f00');

JSONArray := JSONArray.Create;

JSONObject := TJSONObject.Create;
JSONObject.AddPair('colors', JSONArray);



2) New framework

var

StringWriter: TStringWriter;

Writer: TJsonTextWriter;

begin

StrintWriter := TStringWriter();

Writer := TJsonTextWriter.Create(StringWriter);

Writer.WriteStartObject;

Writer.WritePropertyName('colors');

Writer.WriteStartArray;

   Writer.WriteStartObject;

   Writer.WritePropertyName('name');

   Writer.WriteValue('red');

   Writer.WriteEndObject;

   

   Writer.WriteStartObject;

   Writer.WritePropertyName('hex');

   Writer.WriteValue('#f00');

   Writer.WriteEndObject;

Writer.WriteEndArray;

Writer.WriteEndObject;

end;


2. JSON Reader


1) Old framework

JSONValue := TJSONObject.ParseJSONValue('{"color": "red"}');


if JSONValue is TJSONArray then

// ..

else if JSONValue is TJSONObject then

// ..


2) New framwork

StringReader := TStringReader.Create('{"color": "red"}');

Reader := TJsonReader.Create(StringReader);


while not Reader.Read do

case Reader.TokenType of

TJsonToken.StartObject: //..

TJsonToken.StartArray: //..

TJsonToken.Propertyname: //..

TJsonToken.Integer: //..

TJsonToken.EndObject: //..

TJsonToken.EndArray: //..

// .....


4. 'TJSONDocument' component

'FMX > Tip' 카테고리의 다른 글

delphi - SSL Email 발송 (IdSMTP 사용)  (0) 2021.05.27
delphi shortkey  (0) 2021.05.07
FMX - splash Screen 만들기  (0) 2017.12.21
FMX - 인터넷 연결 Check  (0) 2017.08.07
FMX Facebook 연동  (0) 2017.08.04