delphi 实体类 JSON 数组
delphi 实体类 与JSON转换,序列化
TJson.ObjectToJsonString
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
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', ));
fMyPeople.Add(TPerson.Create('Morticia', 'Addams', ));
fMyPeople.Add(TPerson.Create('Pugsley', 'Addams', ));
fMyPeople.Add(TPerson.Create('Wednesday', 'Addams', )); // 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
First declare with Pascal to use the converter and generics. ////
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. ////
#include <System.JSON.Converters.hpp>
#include <System.JSON.Serializers.hpp>
#include <System.Generics.Collections.hpp>
#include <System.JSON.Writers.hpp>
#include <memory>
////
template <typename T1, typename T2>
struct jCollections
{
UnicodeString _property_name{L""};
std::unique_ptr<TStringWriter> _string_write{std::make_unique<TStringWriter>()};
std::unique_ptr<TJsonSerializer> _j_serializer{std::make_unique<TJsonSerializer>()};
std::unique_ptr<TJsonTextWriter> _json_writer{std::make_unique<TJsonTextWriter>(_string_write.get())};
jCollections(UnicodeString lname)
{
///Constructor.
///Determine the property name.
_property_name = lname;
}
String listtoj(T1 l_list)
{
///Start of object and setting property name.
_json_writer->WriteStartObject();
_json_writer->WritePropertyName(_property_name);
///TJsonListConverter__1 or TJsonDictionaryConverterStrStr.
std::unique_ptr<T2 > l_json_conv{std::make_unique<T2 >()}; ///Here convert the value of the input list.
l_json_conv->WriteJson(_json_writer.get(), TValue::_op_Implicit(l_list), _j_serializer.get());
_json_writer->WriteEndObject();
///returns a string.
return _string_write->ToString();
} };
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TList__1<String>* l_list= new TList__1<String>();
try
{
jCollections<TList__1<String>*, TJsonListConverter__1<String> > _jCollections{L"language_List"};
l_list->Add("Python");
l_list->Add("Delphi");
l_list->Add("C++");
l_list->Add("JAVA");
l_list->Add("ひまわり");
Memo1->Lines->Append(_jCollections.listtoj(l_list) );
}
__finally
{
delete l_list;
} }
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
TDictionaryStrStr* l_dic = new TDictionaryStrStr();
try
{
jCollections<TDictionaryStrStr*, TJsonDictionaryConverterStrStr > _jCollections{L"2017_MotoGP_standings"};
l_dic->Add("Maverick Vinales", "Movistar Yamaha");
l_dic->Add("Andrea Dovizioso", "Ducati");
l_dic->Add("Valentino Rossi", "Movistar Yamaha");
l_dic->Add("Marc Marquez", "Repsol Honda Team");
l_dic->Add("Dani Pedrosa", "Repsol Honda Team");
Memo1->Lines->Append(_jCollections.listtoj(l_dic));
}
__finally
{
delete l_dic;
} }
Button1 is TList<String> to JSON Convert. Button2 is TDictionary<String, String>to JSON.
3.数组json
TUser = class
public
FAge: Integer;
FLastName: string;
FFirstName: string; end;
TJsonSerializer的序列化操作,class和 record都支持,使用起来太方便了。
数组2json
procedure TForm3.Button14Click(Sender: TObject);
var
user: TUser;
userList: TArray<TUser>;
begin
user := TUser.Create;
user.FAge := ;
user.FFirstName := 'delphi';
user.FLastName := 'berlin'; SetLength(userList, ); userList[] := user; user := TUser.Create;
user.FFirstName := 'delphi';
user.FAge := ;
user.FLastName := 'tokyo'; userList[] := (user);
self.Memo1.Text := TJsonSerializer.Create.Serialize(userList); end;
json2数组
procedure TForm3.Button15Click(Sender: TObject);
var
userList: TArray<TUser>;
user: TUser;
i: TArray<Integer>;
begin
userList := TJsonSerializer.Create.Deserialize < TArray < TUser >> (Memo1.Text); Memo2.Clear;
for user in userList do
begin
Memo2.Lines.Add(user.FAge.ToString);
Memo2.Lines.Add(user.FFirstName);
Memo2.Lines.Add(user.FLastName);
end; end;
delphi 实体类 JSON 数组的更多相关文章
- Fastjson 实体类JSON化过滤字段操作-PropertyFilter
过滤实体类中年龄等于5的字段 List<Users> models=new ArrayList<>(); for(int i=0;i<11;i++){ Users mod ...
- 实体类Json串转成DataTable
private DataTable GetJsonToDataTable(string json) { List<Object_DeclareInfo> arrayList = JsonC ...
- mvc web api 保存多个实体类的方法
先说一下我的业务需求,挺简单的. 就是在设计角色和权限的页面中,需要一个角色对应多条权限记录,那么在保存的时候,我的处理方式是,先删除该角色的所有权限,然后再保存所有的权限. 这样的话问题就来了,用默 ...
- [LINQ2Dapper]最完整Dapper To Linq框架(三)---实体类关系映射
此特性需要安装Kogel.Dapper.Mssql或者Oracle 3.06及以上版本,实体类层需要安装Kogel.Dapper.Extension 3.06及以上版本 目录 [LINQ2Dapper ...
- JSON-JSON字符串转换成JSON对象、JSON对象数组、java实体类以及保存到List列表中
处理JSON字符串时,一直出错,写个样例后发现原来是没有弄清楚数据的格式问题. 实现的是 JSONString 转换成java对象 或是 list列表 实例类 News package lyx.ent ...
- .NET平台开源项目速览(18)C#平台JSON实体类生成器JSON C# Class Generator
去年,我在一篇文章用原始方法解析复杂字符串,json一定要用JsonMapper么?中介绍了简单的JSON解析的问题,那种方法在当时的环境是非常方便的,因为不需要生成实体类,结构很容易解析.但随着业务 ...
- JSon实体类快速生成插件 GsonFormat 1.2.0
写在前头:本插件只适用 android studio和 Intellij IDEA 工具,eclipse 的少年无视我吧!!! 这是一个根据JSONObject格式的字符串,自动生成实体类参数. gi ...
- 将JSON数据转换成JAVA的实体类
思路:首先将JSON格式的数据转换成JSONObject,然后将JSONObject转换成Java的实体类(其中类属性包括List等类型) Java实体类: SearchFilter 类 1 publ ...
- c# json数据解析——将字符串json格式数据转换成对象或实体类
网络中数据传输经常是xml或者json,现在做的一个项目之前调其他系统接口都是返回的xml格式,刚刚遇到一个返回json格式数据的接口,通过例子由易到难总结一下处理过程,希望能帮到和我一样开始不会的朋 ...
随机推荐
- DCOS(centos 7.4/7.6)
https://dcos.io/releases/ https://downloads.dcos.io/dcos/stable/1.12.0/dcos_generate_config.sh https ...
- UVA-10129 Play on Words (判断欧拉道路的存在性)
题目大意:给出一系列单词,当某个单词的首字母和前一个单词的尾字母相同,则这两个单词能链接起来.给出一系列单词,问是否能够连起来. 题目分析:以单词的首尾字母为点,单词为边建立有向图,便是判断图中是否存 ...
- Codeforces Round #279 (Div. 2) 题解集合
终于有场正常时间的比赛了...毛子换冬令时还正是好啊233 做了ABCD,E WA了3次最后没搞定,F不会= = 那就来说说做的题目吧= = A. Team Olympiad 水题嘛= = 就是个贪心 ...
- spring boot与spring mvc的区别
Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等.但他们的基础都是Spring 的 ioc和 aop ioc 提供了依赖注入的容器 aop ,解决了面向横切面 ...
- C++11_ 右值引用
版权声明:本文为博主原创文章,未经博主允许不得转载. 由于右值所产生的临时变量问题一直是一种诟病,C++11中引入了右值引用这个核心语言机制,来提升运行期性能. 首先我先说明一下什么是左值和右值: 左 ...
- tcpdump学习笔记
简介 简单的说,tcpdump就是一个抓包工具,类似Wireshark. tcpdump可以根据使用者的定义过滤/截取网络上的数据包,并进行分析.tcpdump可以将数据包的头部完全接 ...
- BZOJ2124: 等差子序列(树状数组&hash -> bitset 求是否存在长度为3的等差数列)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 2354 Solved: 826[Submit][Status][Discuss ...
- windows常用DLL及作用
Kernel.dll:内存,硬盘灯硬件管理的相关函数. gdi32.dll:图形显示相关函数(LoadImage,GetPixel,StretchBlt,Ellipse). user32.dll:wi ...
- linux下不同tomcat使用不同的jdk版本
环境:Linux tomcat7 使用jdk7 tomcat 8使用jdk8 最近写好了一个程序,需要部署在测试机器上,这个测试机器上已经有了在运行的tomcat7, 我这个程序要求使用tomcat8 ...
- vue.js权威指南----代码解释实例
1:P61(值绑定) <input type="checkbox" v-model="toggle" :true-value="a" ...