DataSetToJSON
来源:互联网 发布:mac里面怎么卸载程序 编辑:程序博客网 时间:2024/05/20 16:01
unit FMX.DataSetToJSON;
interface
uses
FireDAC.Comp.Client,Data.DB;
function DataSetToJSON(DataSet:TDataSet):String;
function JSONToDataSet(JSONTEXT:String):TFDMemTable;
implementation
uses System.Rtti,System.JSON;
function DataSetToJSON(DataSet:TDataSet):String;
var
I:integer;
JSONObject,FieldJSONObject:TJSONObject;
JSONArray:TJSONArray;
begin
Result:='';
JSONObject:=TJSONObject.ParseJSONValue('{}') as TJSONObject;
try
JSONObject.AddPair('Fields',TJSONArray.Create);
JSONArray:=JSONObject.GetValue('Fields') as TJSONArray;
for I := 0 to DataSet.FieldDefs.Count -1 do
begin
FieldJSONObject:=TJSONObject.Create ;
FieldJSONObject.AddPair ('FieldName',DataSet.FieldDefs[i].Name );
FieldJSONObject.AddPair('DataType',TRttiEnumerationType.GetName<TFieldType>(DataSet.FieldDefs[i].DataType));
FieldJSONObject.AddPair('DataSize',TJSONNumber.Create(DataSet.FieldDefs[i].Size));
JSONArray.Add(FieldJSONObject) ;
end;
DataSet.First ;
JSONObject.AddPair('DATAS',TJSONArray.Create);
JSONArray:=JSONObject.GetValue('DATAS') as TJSONArray;
while not DataSet.Eof do
begin
FieldJSONObject:=TJSONObject.Create ;
for I := 0 to DataSet.FieldDefs.Count -1 do
FieldJSONObject.AddPair(DataSet.FieldDefs[i].Name,DataSet.Fields[i].AsString);
JSONArray.Add(FieldJSONObject) ;
DataSet.Next ;
end;
Result := JSONObject.ToJSON ;
finally
JSONObject.Free ;
end;
end;
function JSONToDataSet(JSONTEXT:String):TFDMemTable;
var
I,R:integer;
JSONObject,FieldJSONObject:TJSONObject;
JSONArray:TJSONArray;
DataName,DataType:String;
FieldType:TFieldType;
FieldSize:integer;
begin
Result:=TFDMemTable.Create(nil);
if JSONTEXT='' then Exit;
JSONObject:=TJSONObject.ParseJSONValue(JSONTEXT) as TJSONObject;
try
JSONArray:=JSONObject.GetValue('Fields') as TJSONArray;
for I := 0 to JSONArray.size -1 do
begin
Result.FieldDefs.Add(((JSONArray.Get(i) as TJSONObject).GetValue('FieldName') as TJSONString).Value,
TRttiEnumerationType.GetValue<TFieldType>(((JSONArray.Get(i) as TJSONObject).GetValue('DataType') as TJSONString).Value),
((JSONArray.Get(i) as TJSONObject).GetValue('DataSize') as TJSONNumber).AsInt64);
end;
Result.CreateDataSet ;
JSONArray:=JSONObject.GetValue('DATAS') as TJSONArray;
for I := 0 to JSONArray.size -1 do
begin
Result.Append ;
for R := 0 to Result.FieldDefs.Count -1 do
begin
FieldJSONObject:=(JSONArray.Get(i) as TJSONObject);
if FieldJSONObject=nil then continue;
try
Result.FieldByName(Result.FieldDefs[R].Name).Value :=
(FieldJSONObject.GetValue(Result.FieldDefs[R].Name) as TJSONString).Value ;
Except
end;
end;
Result.Post ;
end;
finally
JSONObject.Free ;
end;
end;
end.
interface
uses
FireDAC.Comp.Client,Data.DB;
function DataSetToJSON(DataSet:TDataSet):String;
function JSONToDataSet(JSONTEXT:String):TFDMemTable;
implementation
uses System.Rtti,System.JSON;
function DataSetToJSON(DataSet:TDataSet):String;
var
I:integer;
JSONObject,FieldJSONObject:TJSONObject;
JSONArray:TJSONArray;
begin
Result:='';
JSONObject:=TJSONObject.ParseJSONValue('{}') as TJSONObject;
try
JSONObject.AddPair('Fields',TJSONArray.Create);
JSONArray:=JSONObject.GetValue('Fields') as TJSONArray;
for I := 0 to DataSet.FieldDefs.Count -1 do
begin
FieldJSONObject:=TJSONObject.Create ;
FieldJSONObject.AddPair ('FieldName',DataSet.FieldDefs[i].Name );
FieldJSONObject.AddPair('DataType',TRttiEnumerationType.GetName<TFieldType>(DataSet.FieldDefs[i].DataType));
FieldJSONObject.AddPair('DataSize',TJSONNumber.Create(DataSet.FieldDefs[i].Size));
JSONArray.Add(FieldJSONObject) ;
end;
DataSet.First ;
JSONObject.AddPair('DATAS',TJSONArray.Create);
JSONArray:=JSONObject.GetValue('DATAS') as TJSONArray;
while not DataSet.Eof do
begin
FieldJSONObject:=TJSONObject.Create ;
for I := 0 to DataSet.FieldDefs.Count -1 do
FieldJSONObject.AddPair(DataSet.FieldDefs[i].Name,DataSet.Fields[i].AsString);
JSONArray.Add(FieldJSONObject) ;
DataSet.Next ;
end;
Result := JSONObject.ToJSON ;
finally
JSONObject.Free ;
end;
end;
function JSONToDataSet(JSONTEXT:String):TFDMemTable;
var
I,R:integer;
JSONObject,FieldJSONObject:TJSONObject;
JSONArray:TJSONArray;
DataName,DataType:String;
FieldType:TFieldType;
FieldSize:integer;
begin
Result:=TFDMemTable.Create(nil);
if JSONTEXT='' then Exit;
JSONObject:=TJSONObject.ParseJSONValue(JSONTEXT) as TJSONObject;
try
JSONArray:=JSONObject.GetValue('Fields') as TJSONArray;
for I := 0 to JSONArray.size -1 do
begin
Result.FieldDefs.Add(((JSONArray.Get(i) as TJSONObject).GetValue('FieldName') as TJSONString).Value,
TRttiEnumerationType.GetValue<TFieldType>(((JSONArray.Get(i) as TJSONObject).GetValue('DataType') as TJSONString).Value),
((JSONArray.Get(i) as TJSONObject).GetValue('DataSize') as TJSONNumber).AsInt64);
end;
Result.CreateDataSet ;
JSONArray:=JSONObject.GetValue('DATAS') as TJSONArray;
for I := 0 to JSONArray.size -1 do
begin
Result.Append ;
for R := 0 to Result.FieldDefs.Count -1 do
begin
FieldJSONObject:=(JSONArray.Get(i) as TJSONObject);
if FieldJSONObject=nil then continue;
try
Result.FieldByName(Result.FieldDefs[R].Name).Value :=
(FieldJSONObject.GetValue(Result.FieldDefs[R].Name) as TJSONString).Value ;
Except
end;
end;
Result.Post ;
end;
finally
JSONObject.Free ;
end;
end;
end.
0 0
- DataSetToJSON
- C# 当通讯数据比较多,需要分组发送,对DataSetToJson的重载
- Java对象的深复制和浅复制
- UVA 796 Critical Links (桥)
- CodeForces 288C
- 基于JIRE做的一个项目度量平台
- Mongodb 3节点 Rep set +sharding 配置
- DataSetToJSON
- 专访程炳皓:八年一觉“开心”梦
- flex布局详解
- 指针数组
- 使用腾讯TAB做成的第一个demo
- kernel 源码中的 ACCESS_ONCE()
- JSP与Servlet关系
- 初学Struts2第一天,学习Struts基础配置,第一次写博文,写的不好勿喷哦
- C语言做的接鸡蛋小游戏(附源码注释)【原创】