delphi json
生活随笔
收集整理的這篇文章主要介紹了
delphi 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":[記錄集信息]
廢話少說,直接上代碼:unit uDBJson;
interface
uses
SysUtils,Classes,Variants,DB,DBClient,SuperObject;
type
TTableJSon = class
private
? ? const cstFieldType = 'FieldType';
? ? const cstFieldName = 'FieldName';
? ? const cstFieldSize = 'FieldSize';
? ? const cstJsonType = 'JsonType';
? ? const cstRequired = 'Required';
? ? const cstFieldIndex = 'FieldIndex';
? ? const cstCols= 'Cols';
? ? const cstData= 'Data';
public
? ? class function JSonFromDataSet(DataSet:TDataSet):string;
? ? class function CreateFieldByJson(Fields:TFieldDefs;ColsJson:ISuperObject):Boolean;
? ? class function ImportDataFromJSon(DataSet:TDataSet;DataJson:ISuperObject):Integer;
? ? class function CDSFromJSon(CDS:TClientDataSet;Json:ISuperObject):Boolean;
? ? class function GetValue(Json:ISuperObject;const Name:string):Variant;
? ? class function CreateJsonValue(Json:ISuperObject;const Name:string;const Value:Variant):Boolean;
? ? class function CreateJsonValueByField(Json:ISuperObject;Field:TField):Boolean;
? ? class function GetValue2Field(Field:TField;JsonValue:ISuperObject):Variant;
end;
implementation
uses TypInfo,encddecd;
{ TTableJSon }
class function TTableJSon.CDSFromJSon(CDS: TClientDataSet;
Json: ISuperObject): Boolean;
var
ColsJson:ISuperObject;
begin
Result := False;
if Json = nil then
? ? Exit;
CDS.Close;
CDS.Data := Null;
//創建字段
ColsJson := Json.O[cstCols];
CreateFieldByJson(CDS.FieldDefs,ColsJson);
if CDS.FieldDefs.Count >0 then
? ? CDS.CreateDataSet;
ImportDataFromJSon(CDS,Json.O[cstData]);
Result := True;
end;
class function TTableJSon.CreateFieldByJson(Fields: TFieldDefs;
ColsJson: ISuperObject): Boolean;
var
SubJson:ISuperObject;
ft:TFieldType;
begin
Result := False;
Fields.DataSet.Close;
Fields.Clear;
for SubJson in ColsJson do
begin
? ? ft := TFieldType(GetEnumValue(TypeInfo(TFieldType),'ft'+SubJson.S[cstFieldType]));
? ? if ft= ftAutoInc then //自增字段不能錄入,必須更改
? ?? ?ft := ftInteger;
? ? Fields.Add(SubJson.S[cstFieldName],ft,SubJson.I[cstFieldSize],SubJson.B[cstRequired]);
end;
Result := True;
end;
class function TTableJSon.CreateJsonValue(Json: ISuperObject;
const Name: string; const Value: Variant): Boolean;
begin
Result := False;
Json.O[Name] := SO(Value);
Result := True;
end;
class function TTableJSon.CreateJsonValueByField(Json: ISuperObject;
Field: TField): Boolean;
begin
Result := False;
if Field Is TDateTimeField then
? ? Json.O[Field.FieldName] := SO(Field.AsDateTime)
else if Field is TBlobField then
? ? Json.S[Field.FieldName] := EncodeString(Field.AsString)
else
? ? Json.O[Field.FieldName] := SO(Field.Value);
Result := True;
end;
class function TTableJSon.GetValue(
Json: ISuperObject;const Name: string): Variant;
begin
case Json.DataType of
? ? 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;
class function TTableJSon.GetValue2Field(Field: TField; JsonValue:ISuperObject): Variant;
begin
if JsonValue.DataType = stNull then
? ? Result := Null
else if Field is TDateTimeField then
? ? Result := JavaToDelphiDateTime(JsonValue.AsInteger)
else if (Field is TIntegerField) or (Field is TLargeintField) then
? ? Result := JsonValue.AsInteger
else if Field is TNumericField then
? ? Result := JsonValue.AsDouble
else if Field is TBooleanField then
? ? Result := JsonValue.AsBoolean
else if Field is TStringField then
? ? Result := JsonValue.AsString
else if Field is TBlobField then
? ? Result := DecodeString(JsonValue.AsString)? ?
end;
class function TTableJSon.ImportDataFromJSon(DataSet: TDataSet;
DataJson: ISuperObject): Integer;
var
SubJson:ISuperObject;
i:Integer;
iter: TSuperObjectIter;
begin
if not DataSet.Active then
? ? DataSet.Open;
DataSet.DisableControls;
try
? ? for SubJson in DataJson do
? ? begin
? ?? ?DataSet.Append;
? ?? ?if ObjectFindFirst(SubJson,iter) then
? ?? ?begin
? ?? ?? ?repeat
? ?? ?? ???if DataSet.FindField(iter.Ite.Current.Name)<>nil then
? ?? ?? ?? ? DataSet.FindField(iter.Ite.Current.Name).Value :=
? ?? ?? ?? ?? ? GetValue2Field(
? ?? ?? ?? ?? ? DataSet.FindField(iter.Ite.Current.Name),
? ?? ?? ?? ?? ? iter.Ite.Current.Value);
? ?? ?? ?until not ObjectFindNext(iter) ;
? ?? ?end;
? ?? ?DataSet.Post;
? ? end;
finally
? ? DataSet.EnableControls;
end;
end;
class function TTableJSon.JSonFromDataSet(DataSet:TDataSet):string;
procedure GetFieldTypeInfo(Field:TField;var Fieldtyp,JsonTyp:string);
begin
? ? Fieldtyp := GetEnumName(TypeInfo(tfieldtype),ord(Field.DataType));
? ? Delete(Fieldtyp,1,2);
? ? if Field is TStringField then
? ?? ?JsonTyp := 'string'
? ? else if Field is TDateTimeField then
? ?? ?JsonTyp := 'integer'
? ? else if (Field is TIntegerField) or (Field is TLargeintField) then
? ?? ?JsonTyp := 'integer'
? ? else if Field is TCurrencyField then
? ?? ?JsonTyp := 'currency'
? ? else if Field is TNumericField then
? ?? ?JsonTyp := 'double'
? ? else if Field is TBooleanField then
? ?? ?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;
? ?
? ? for i := 0 to DataSet.FieldCount - 1 do
? ? 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([]);
? ? while not DataSet.Eof do
? ? begin
? ?? ?sj2 := SO();
? ?? ?for i := 0 to DataSet.FieldCount - 1 do
? ?? ?begin
? ?? ???//sj2.S[IntToStr(DataSet.Fields[i].Index)] := VarToStrDef(DataSet.Fields[i].Value,'');
? ?? ???if VarIsNull(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必須也建立字典信息,才能創建數據集的字段信息。我們設置其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":[記錄集信息]
廢話少說,直接上代碼:
總結
以上是生活随笔為你收集整理的delphi json的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 要害怕做事的s9t9
- 下一篇: 程序开发部门的“飞鸽传书”文件柜上面