Delphi中JSonSuperObject使用数据集与JSON对象互转.docx
- 文档编号:9186741
- 上传时间:2023-02-03
- 格式:DOCX
- 页数:30
- 大小:23.93KB
Delphi中JSonSuperObject使用数据集与JSON对象互转.docx
《Delphi中JSonSuperObject使用数据集与JSON对象互转.docx》由会员分享,可在线阅读,更多相关《Delphi中JSonSuperObject使用数据集与JSON对象互转.docx(30页珍藏版)》请在冰豆网上搜索。
Delphi中JSonSuperObject使用数据集与JSON对象互转
在delphi中,数据集是最常用数据存取方式。
因此,必须建立JSON与TDataSet之间的互转关系,实现数据之间通讯与转换。
值得注意的是,这只是普通的TDataset与JSON之间转换,由于CDS包含了Delta数据包,其数据格式远比普通的TDataset更复杂。
数据集字段信息,是一个完整的字典信息。
因此,我们在JSON必须也建立字典信息,才能创建数据集的字段信息。
我们设置其JSON信息如下:
COLS:
[字段列表信息],如:
"Cols":
[{"JsonType":
"integer","FieldIndex":
0,"FieldType":
"Integer","FieldSize":
0,"FieldName":
"ID","Required":
false},{"JsonType":
"string","FieldIndex":
1,"FieldType":
"String","FieldSize":
100,"FieldName":
"Title","Required":
false},{"JsonType":
"variant","FieldIndex":
2,"FieldType":
"Blob","FieldSize":
0,"FieldName":
"Picture","Required":
false}]
数据信息以Data做节点,也是一个数组嵌套记录信息:
"Data":
[记录集信息]
废话少说,直接上代码:
unituDBJson;
interface
uses
SysUtils,Classes,Variants,DB,DBClient,SuperObject;
type
TTableJSon=class
private
constcstFieldType='FieldType';
constcstFieldName='FieldName';
constcstFieldSize='FieldSize';
constcstJsonType='JsonType';
constcstRequired='Required';
constcstFieldIndex='FieldIndex';
constcstCols='Cols';
constcstData='Data';
public
classfunctionJSonFromDataSet(DataSet:
TDataSet):
string;
classfunctionCreateFieldByJson(Fields:
TFieldDefs;ColsJson:
ISuperObject):
Boolean;
classfunctionImportDataFromJSon(DataSet:
TDataSet;DataJson:
ISuperObject):
Integer;
classfunctionCDSFromJSon(CDS:
TClientDataSet;Json:
ISuperObject):
Boolean;
classfunctionGetValue(Json:
ISuperObject;constName:
string):
Variant;
classfunctionCreateJsonValue(Json:
ISuperObject;constName:
string;constValue:
Variant):
Boolean;
classfunctionCreateJsonValueByField(Json:
ISuperObject;Field:
TField):
Boolean;
classfunctionGetValue2Field(Field:
TField;JsonValue:
ISuperObject):
Variant;
end;
implementation
usesTypInfo,encddecd;
{TTableJSon}
classfunctionTTableJSon.CDSFromJSon(CDS:
TClientDataSet;
Json:
ISuperObject):
Boolean;
var
ColsJson:
ISuperObject;
begin
Result:
=False;
ifJson=nilthen
Exit;
CDS.Close;
CDS.Data:
=Null;
//创建字段
ColsJson:
=Json.O[cstCols];
CreateFieldByJson(CDS.FieldDefs,ColsJson);
ifCDS.FieldDefs.Count>0then
CDS.CreateDataSet;
ImportDataFromJSon(CDS,Json.O[cstData]);
Result:
=True;
end;
classfunctionTTableJSon.CreateFieldByJson(Fields:
TFieldDefs;
ColsJson:
ISuperObject):
Boolean;
var
SubJson:
ISuperObject;
ft:
TFieldType;
begin
Result:
=False;
Fields.DataSet.Close;
Fields.Clear;
forSubJsoninColsJsondo
begin
ft:
=TFieldType(GetEnumValue(TypeInfo(TFieldType),'ft'+SubJson.S[cstFieldType]));
ifft=ftAutoIncthen//自增字段不能录入,必须更改
ft:
=ftInteger;
Fields.Add(SubJson.S[cstFieldName],ft,SubJson.I[cstFieldSize],SubJson.B[cstRequired]);
end;
Result:
=True;
end;
classfunctionTTableJSon.CreateJsonValue(Json:
ISuperObject;
constName:
string;constValue:
Variant):
Boolean;
begin
Result:
=False;
Json.O[Name]:
=SO(Value);
Result:
=True;
end;
classfunctionTTableJSon.CreateJsonValueByField(Json:
ISuperObject;
Field:
TField):
Boolean;
begin
Result:
=False;
ifFieldIsTDateTimeFieldthen
Json.O[Field.FieldName]:
=SO(Field.AsDateTime)
elseifFieldisTBlobFieldthen
Json.S[Field.FieldName]:
=EncodeString(Field.AsString)
else
Json.O[Field.FieldName]:
=SO(Field.Value);
Result:
=True;
end;
classfunctionTTableJSon.GetValue(
Json:
ISuperObject;constName:
string):
Variant;
begin
caseJson.DataTypeof
stNull:
Result:
=Null;
stBoolean:
Result:
=Json.B[Name];
stDouble:
Result:
=Json.D[Name];
stCurrency:
Result:
=Json.C[Name];
stInt:
Result:
=Json.I[Name];
stString:
Result:
=Json.S[Name];
end;
end;
classfunctionTTableJSon.GetValue2Field(Field:
TField;JsonValue:
ISuperObject):
Variant;
begin
ifJsonValue.DataType=stNullthen
Result:
=Null
elseifFieldisTDateTimeFieldthen
Result:
=JavaToDelphiDateTime(JsonValue.AsInteger)
elseif(FieldisTIntegerField)or(FieldisTLargeintField)then
Result:
=JsonValue.AsInteger
elseifFieldisTNumericFieldthen
Result:
=JsonValue.AsDouble
elseifFieldisTBooleanFieldthen
Result:
=JsonValue.AsBoolean
elseifFieldisTStringFieldthen
Result:
=JsonValue.AsString
elseifFieldisTBlobFieldthen
Result:
=DecodeString(JsonValue.AsString)
end;
classfunctionTTableJSon.ImportDataFromJSon(DataSet:
TDataSet;
DataJson:
ISuperObject):
Integer;
var
SubJson:
ISuperObject;
i:
Integer;
iter:
TSuperObjectIter;
begin
ifnotDataSet.Activethen
DataSet.Open;
DataSet.DisableControls;
try
forSubJsoninDataJsondo
begin
DataSet.Append;
ifObjectFindFirst(SubJson,iter)then
begin
repeat
ifDataSet.FindField(iter.Ite.Current.Name)<>nilthen
DataSet.FindField(iter.Ite.Current.Name).Value:
=
GetValue2Field(
DataSet.FindField(iter.Ite.Current.Name),
iter.Ite.Current.Value);
untilnotObjectFindNext(iter);
end;
DataSet.Post;
end;
finally
DataSet.EnableControls;
end;
end;
classfunctionTTableJSon.JSonFromDataSet(DataSet:
TDataSet):
string;
procedureGetFieldTypeInfo(Field:
TField;varFieldtyp,JsonTyp:
string);
begin
Fieldtyp:
=GetEnumName(TypeInfo(tfieldtype),ord(Field.DataType));
Delete(Fieldtyp,1,2);
ifFieldisTStringFieldthen
JsonTyp:
='string'
elseifFieldisTDateTimeFieldthen
JsonTyp:
='integer'
elseif(FieldisTIntegerField)or(FieldisTLargeintField)then
JsonTyp:
='integer'
elseifFieldisTCurrencyFieldthen
JsonTyp:
='currency'
elseifFieldisTNumericFieldthen
JsonTyp:
='double'
elseifFieldisTBooleanFieldthen
JsonTyp:
='boolean'
else
JsonTyp:
='variant';
end;
var
sj,aj,sj2:
ISuperObject;
i:
Integer;
Fieldtyp,JsonTyp:
string;
List:
TStringList;
begin
sj:
=SO();
//创建列
aj:
=SA([]);
List:
=TStringList.Create;
try
List.Sorted:
=True;
fori:
=0toDataSet.FieldCount-1do
begin
sj2:
=SO();
GetFieldTypeInfo(DataSet.Fields[i],Fieldtyp,JsonTyp);
sj2.S[cstFieldName]:
=DataSet.Fields[i].FieldName;
sj2.S[cstFieldType]:
=Fieldtyp;
sj2.S[cstJsonType]:
=JsonTyp;
sj2.I[cstFieldSize]:
=DataSet.Fields[i].Size;
sj2.B[cstRequired]:
=DataSet.Fields[i].Required;
sj2.I[cstFieldIndex]:
=DataSet.Fields[i].Index;
aj.AsArray.Add(sj2);
List.Add(DataSet.Fields[i].FieldName+'='+JsonTyp);
end;
sj.O['Cols']:
=aj;
//创建数据集的数据
DataSet.DisableControls;
DataSet.First;
aj:
=SA([]);
whilenotDataSet.Eofdo
begin
sj2:
=SO();
fori:
=0toDataSet.FieldCount-1do
begin
//sj2.S[IntToStr(DataSet.Fields[i].Index)]:
=VarToStrDef(DataSet.Fields[i].Value,'');
ifVarIsNull(DataSet.Fields[i].Value)then
sj2.O[DataSet.Fields[i].FieldName]:
=SO(Null)
else
begin
CreateJsonValueByField(sj2,DataSet.Fields[i]);
end;
end;
aj.AsArray.Add(sj2);
DataSet.Next;
end;
sj.O['Data']:
=aj;
Result:
=sj.AsString;
finally
List.Free;
DataSet.EnableControls;
end;
end;
end.
调用示例:
//数据集转JSON对象或JSON文本
var
json:
TTableJSon;
s:
string;
begin
S:
=json.JSonFromDataSet(ADODataSet1);
//在用TStringStream读入字符串S,存成文本,看看其格式.
end;
//JSON对象或文本,装载到数据集
var
json:
ISuperObject;
begin
json:
=TSuperObject.ParseFile('json.txt',False);
TTableJSon.CDSFromJSon(cdsJSON,json);
end;
JSON不能完全替代XML,但绝对是未来的大势所趋,其优点是简单、体积小、解析更快、解析占用资源更少。
在delphi中,数据集是最常用数据存取方式。
因此,必须建立JSON与TDataSet之间的互转关系,实现数据之间通讯与转换。
值得注意的是,这只是普通的TDataset与JSON之间转换,由于CDS包含了Delta数据包,其数据格式远比普通的TDataset更复杂。
下面的程序,或许你有不同的想法,如果你的想法更好更快,欢迎一起讨论。
今天是2009年最后的几十分钟,重要的并不是写博客,而是想向大家说一声“新年好运,事业有成”!
数据集字段信息,是一个完整的字典信息。
因此,我们在JSON必须也建立字典信息,才能创建数据集的字段信息。
我们设置其JSON信息如下:
COLS:
[字段列表信息],如:
"Cols":
[{"JsonType":
"integer","FieldIndex":
0,"FieldType":
"Integer","FieldSize":
0,"FieldName":
"ID","Required":
false},{"JsonType":
"string","FieldIndex":
1,"FieldType":
"String","FieldSize":
100,"FieldName":
"Title","Required":
false},{"JsonType":
"variant","FieldIndex":
2,"FieldType":
"Blob","FieldSize":
0,"FieldName":
"Picture","Required":
false}]
数据信息以Data做节点,也是一个数组嵌套记录信息:
"Data":
[记录集信息]
废话少说,直接上代码:
unituDBJson;
interface
uses
SysUtils,Classes,Variants,DB,DBClient,SuperObject;
type
TTableJSon=class
private
constcstFieldType='FieldType';
constcstFieldName='FieldName';
constcstFieldSize='FieldSize';
constcstJsonType='JsonType';
constcstRequired='Required';
constcstFieldIndex='FieldIndex';
constcstCols='Cols';
constcstData='Data';
public
classfunctionJSonFromDataSet(DataSet:
TDataSet):
string;
classfunctionCreateFieldByJson(Fields:
TFieldDefs;ColsJson:
ISuperObject):
Boolean;
classfunctionImportDataFromJSon(DataSet:
TDataSet;DataJson:
ISuperObject):
Integer;
classfunctionCDSFromJSon(CDS:
TClientDataSet;Json:
ISuperObject):
Boolean;
classfunctionGetValue(Json:
ISuperObject;constName:
string):
Variant;
classfunctionCreateJsonValue(Json:
ISuperObject;constName:
string;constValue:
Variant):
Boolean;
classfunctionCreateJsonValueByField(Json:
ISuperObject;Field:
TField):
Boolean;
classfunctionGetValue2Field(Field:
TField;JsonValue:
ISuperObject):
Variant;
end;
implementation
usesTypInfo,encddecd;
{TTableJSon}
classfunctionTTableJSon.CDSFromJSon(CDS:
TClientDataSet;
Json:
ISuperObject):
Boolean;
var
ColsJson:
ISuperObject;
begin
Result:
=False;
ifJson=nilthen
Exit;
CDS.Close;
CDS.Data:
=Null;
//创建字段
ColsJson:
=Json.O[cstCols];
CreateFieldByJson(CDS.FieldDefs,ColsJson);
ifCDS.FieldDefs.Count>0then
CDS.CreateDataSet;
ImportDataFromJSon(CDS,Json.O[cstData]);
Result:
=True;
end;
classfunctionTTableJSon.CreateFieldByJson(Fields:
TFieldDefs;
ColsJson:
ISuperObject):
Boolean;
var
SubJson:
ISuperObject;
ft:
TFieldType;
begin
Result:
=False;
Fields.DataSet.Close;
Fields.Clear;
forSubJsoninColsJsondo
begin
ft:
=TFieldType(GetEnumValue(TypeInfo(TFieldType),'ft'+SubJson.S[cstFieldType]));
ifft=ftAutoIncthen//自增字段不能录入,必须更改
ft:
=ftInteger;
Fields.Add(SubJson.S[cstFieldName],ft,SubJson.I[cstFieldSize],SubJson.B[cstRequired]);
end;
Result:
=True;
end;
classfunctionTTableJSon.CreateJsonValue(Json:
ISuperObject;
constName:
string;constValue:
Variant):
Boolean;
begin
Result:
=F
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Delphi JSonSuperObject 使用 数据 JSON 对象