delphi 实体类 与JSON转换,序列化

TJson
REST.JSON.pas
System.JSON.Serializers,
 
TJson.JsonToObject
TJson.ObjectToJsonString
JsonEncode
ObjectToJsonObject
 
http://docwiki.embarcadero.com/Libraries/Seattle/en/REST.Json.TJson_Methods
 
 
 从JSONArray中获得JSONObject对象
   JSONObject   jsonObject = (JSONObject)jsonArray.get(i);
 
从json反序列化为数组
  oup_List:=  TJsonSerializer.Create.Deserialize<TArray<TODPatient>>( jo.GetValue<TJSONArray>('response.message.items').ToString);

TJsonReader\TJsonTextWriter

uses System.JSON.Readers,System.JSON.Writers

http://docwiki.embarcadero.com/Libraries/Seattle/en/System.JSON.Readers

http://docwiki.embarcadero.com/Libraries/Seattle/en/System.JSON.Writers

 

http://docwiki.embarcadero.com/Libraries/Seattle/en/System.JSON.Readers.TJsonReader

http://docwiki.embarcadero.com/Libraries/Seattle/en/System.JSON.Writers.TJsonTextWriter

http://docwiki.embarcadero.com/Libraries/Seattle/en/System.JSON.Readers.TJsonReader_Methods
http://docwiki.embarcadero.com/Libraries/Seattle/en/System.JSON.Writers.TJsonTextWriter_Methods
 
来源
http://www.malcolmgroves.com/blog/?p=1084
http://stackoverflow.com/questions/21528552/how-to-bind-contained-objects-with-live-binding
https://community.embarcadero.com/blogs/entry/xe3-visual-livebindings-user-defined-c-objects-32002
 

unit CollectionObjects;

interface

type
TPerson = class private FAge: Integer; FLastName: string; FFirstName: string; public constructor Create(const FirstName, LastName: String; Age: Integer); property FirstName: string read FFirstName write FFirstName; property LastName: string read FLastName write FLastName; property Age: Integer read FAge write FAge; end; implementation { TPerson } constructor TPerson.Create(const FirstName, LastName: String; Age: Integer); begin FFirstName := FirstName; FLastName := LastName; FAge := Age; end;

 

fMyPeople: TObjectList<TPerson>;

fMyPeople := TObjectList<TPerson>.Create(True);

// The individual TPerson objects
fMyPeople.Add(TPerson.Create('Gomez', 'Addams', 40));
fMyPeople.Add(TPerson.Create('Morticia', 'Addams', 38));
fMyPeople.Add(TPerson.Create('Pugsley', 'Addams', 8));
fMyPeople.Add(TPerson.Create('Wednesday', 'Addams', 12));



// Use TObjectBindSourceAdapter for a single object
ABindSourceAdapter := TListBindSourceAdapter<TPerson>.Create(Self, FMyPeople, True);

function PrettyJSON(AJson: String): String;
begin
Result := StringReplace(AJson, '},', '},' + sLineBreak, [rfReplaceAll]);
Result := StringReplace(Result, '[{', '[' + sLineBreak + '{', [rfReplaceAll]);
end;

procedure TABSMainForm.JsonToObjects;
var
NewPeople: TObjectList<TPerson>;
begin
NewPeople := TJson.JsonToObject<TObjectList<TPerson>>(Memo1.Text);
fMyPeople.Clear;
fMyPeople.AddRange(NewPeople.ToArray);
end;

procedure TABSMainForm.ObjectsToJson;
begin
Memo1.Text := PrettyJSON(TJson.ObjectToJsonString(fMyPeople));
end;

 JsonToObject

ObjectToJsonString

JSON字符串转换为对象

{"ownsObjects":true,"listHelper":[9],"items":[
{"age":40,"lastName":"Addams","firstName":"Gomez"},
{"age":38,"lastName":"Addams","firstName":"Morticia"},
{"age":8,"lastName":"Addams","firstName":"Pugsley"},
{"age":12,"lastName":"Addams","firstName":"Wednesday"},
{"age":55,"lastName":"Fester","firstName":"Uncle"},
{"age":72,"lastName":"Frump","firstName":"Grandmama"},
{"age":50,"lastName":"Lurch","firstName":""},
{"age":99,"lastName":"Thing","firstName":"Thing T."},
{"age":21,"lastName":"Itt","firstName":"Cousin"},
null,null,null,null,null,null,null]}

 TJson::ObjectToJsonString();

 

数据集转为Json

DataSetConverter4Delphi

https://github.com/ezequieljuliano/DataSetConverter4Delphi

 

Object>JsonString 类对象序列化为json字符串。

TPerson=class()....

string astr:= TJson.ObjectToJsonString(person);

JsonString反序列化 实例化为类对象

person := TJson.JsonToObject<TPerson>(astr);

 

Tokyo 10.2新增类,效率更高更快。

TJsonSerializer

Serializer:=TJsonSerializer.Create

String astr=Serializer.Serialize<TPerson>(aperson);

person= nSeriallizer.Deserialize<T>(astring);

 

感觉TJsonSerializer与.net的开源Newtonsoft.Json类似,功能基本相同了。

TJsonSerializer在c++builder的应用

https://community.embarcadero.com/blogs/entry/10-2-tokyo-tjsonserializer-and-json-converters

delphi 实体类 JSON  数组 TJsonSerializer Deserialize
First declare with Pascal to use the converter and generics.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    
////
unit Unit2;
 
interface
uses
  System.JSON.Converters, System.JSON.Serializers, System.Generics.Collections;
type
  TListString = TList<String>;
  TDictionaryStrStr = class(TDictionary<String, String>)
    constructor Create; overload;
 
  end;
  TTJsonListConverterString = TJsonListConverter<String>;
  TJsonDictionaryConverterStrStr = class(TJsonStringDictionaryConverter<String>)
  end;

This is because the use of generics in C++Builder. C++Builder used Win64.
1
2
3
4
5
6
    
////
#include <System.JSON.Converters.hpp>
#include <System.JSON.Serializers.hpp>
#include <System.Generics.Collections.hpp>
#include <System.JSON.Writers.hpp>
#include <memory>
View Code

相关文章:

  • 2022-12-23
  • 2021-06-20
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-11
猜你喜欢
  • 2022-12-23
  • 2021-11-28
  • 2021-09-19
  • 2021-12-29
  • 2021-06-21
  • 2022-12-23
相关资源
相似解决方案