Home > Back-end >  Under the TClientDataSet xe2 and transfers between Json?
Under the TClientDataSet xe2 and transfers between Json?

Time:09-27

Under the TClientDataSet xe2 and transfers between Json? The best example is given,

CodePudding user response:

Internet has a lot of examples...
 
The unit uJSONDB;

Interface
USES the
SysUtils, Classes, Variants, DB, DBClient SuperObject, Dialogs.
Type
TJSONDB=class

Private
The class function getJsonFieldNames (res: ISuperObject) : TStringList;
The class function getJsonFieldValues (res: ISuperObject) : TStringList;
Public
The class procedure JsonToClientDataSet (jsonArr: TSuperArray; DstCDS: the TClientDataSet);
The class function ClientDataSetToJSON (srcCDS: the TClientDataSet) : UTF8String;
The end;

Implementation

The function GetToken (var astring: string; Const FMT: an array of char) : string;
Var
I, j: integer;
Found: Boolean;
The begin
Found:=false;
Result:=';
AString:=TrimLeft (aString);

If length (astring)=0 then the exit;

I:=1;
While I<=length (Astring) do
The begin
Found:=false;
If aString [I] <=# 128 then
The begin
For j:=Low (Fmt) to High (Fmt) do
The begin
If (astring [I] <> Fmt [j]) then continue;
Found:=true;
break;
The end;
If Not found then I:=I + 1;
End
Else I:=I + 2;

If found then break;
The end;

If found then
The begin
Result:=copy (astring, 1, I - 1);
Delete (astring, 1, I);
End
The else
The begin
Result:=astring;
Astring:=';
The end;
The end;

The function GetFieldParams (PropName, Source: string) : string;
Var
S1, S2: string;
TmpParam: string;
AChar: string;
AValue aPropName, aSource: string;
The begin
Result:=';
If Source="' then the Exit;
ASource:=Source;
While aSource & lt;> ' 'do
The begin
AValue:=GetToken (aSource, (', '));
APropName:=GetToken (aValue, [' : ']);
If CompareText (PropName, aPropName) & lt;> 0 then the continue;
Result:=aValue;
break;
The end;
The end;
//obtained from json field name
The class function TJSONDB. GetJsonFieldNames (res: ISuperObject) : TStringList;
Var
I: Integer;
FieldList: TStringList;
FieldNames: String;
The begin
Try
FieldList:=TStringList. Create;
FieldNames:=res AsObject. GetNames. AsString;
FieldNames:=StringReplace (fieldNames, '[',' ', [rfReplaceAll rfIgnoreCase]);
FieldNames:=StringReplace (fieldNames, '] ', ' ', [rfReplaceAll rfIgnoreCase]);
FieldNames:=StringReplace (fieldNames, '"', ' ', [rfReplaceAll rfIgnoreCase]);

FieldList. Delimiter:=', ';
FieldList. DelimitedText:=fieldNames;
Result:=fieldList;
The finally
//fieldList. Free;
The end;
The end;

//field values obtained from the json
The class function TJSONDB. GetJsonFieldValues (res: ISuperObject) : TStringList;
Var
I: Integer;
FieldList: TStringList;
FieldValues: String;
The begin
Try
FieldList:=TStringList. Create;
FieldValues:=res AsObject. GetValues. AsString;
FieldValues:=StringReplace (fieldValues, '[',' ', [rfReplaceAll rfIgnoreCase]);
FieldValues:=StringReplace (fieldValues, '] ', ' ', [rfReplaceAll rfIgnoreCase]);
FieldValues:=StringReplace (fieldValues, '"', ' ', [rfReplaceAll rfIgnoreCase]);

FieldList. Delimiter:=', ';
FieldList. DelimitedText:=fieldValues;
Result:=fieldList;
The finally
//fieldList. Free;
The end;
The end;
//json CDS
The class procedure TJSONDB. JsonToClientDataSet (jsonArr: TSuperArray; DstCDS: the TClientDataSet);
Var
FieldList: TStringList;
ValuesList: TStringList;
JsonSrc: string;
I, j: Integer;
The begin

FieldList:=getJsonFieldNames (SO [jsonArr [0]. AsJson (False, False)]);
If (dstCDS. FieldCount=0) then
The begin
For I:=0 to fieldList. Do the Count - 1
The begin
DstCDS. FieldDefs. Add (fieldList [I], ftString, 100, False);
The end;
DstCDS. CreateDataSet;
DstCDS. Close;
DstCDS. Open;
The end;
Try
DstCDS. DisableControls;
For I:=0 to jsonArr. Do Length - 1
The begin
JsonSrc:=SO [jsonArr [I] AsJson (False, False)]. The AsString;
JsonSrc:=StringReplace (jsonSrc, '[',' ', [rfReplaceAll rfIgnoreCase]);
JsonSrc:=StringReplace (jsonSrc, '] ', ' ', [rfReplaceAll rfIgnoreCase]);
JsonSrc:=StringReplace (jsonSrc, '"', ' ', [rfReplaceAll rfIgnoreCase]);
JsonSrc:=StringReplace (jsonSrc, '{',' ', [rfReplaceAll rfIgnoreCase]);
JsonSrc:=StringReplace (jsonSrc, '} ', ' ', [rfReplaceAll rfIgnoreCase]);
DstCDS. Append;
For j:=0 to fieldList. Do the Count - 1
The begin
DstCDS. FieldByName (fieldList [j].) AsString:=GetFieldParams (fieldList [j], jsonSrc);
The end;
DstCDS. Post;
The end;

The finally
DstCDS. EnableControls;
The end;
The end;

The class function TJSONDB. ClientDataSetToJSON (srcCDS: the TClientDataSet) : UTF8String;
Var
I, j: Integer;
KeyValue: String;
JsonList: TStringList;
JsonResult: String;
The begin
If not srcCDS. Active then srcCDS. Open; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related