您的位置:首页 > Web前端 > JavaScript

clientdataset<---->json

2016-03-02 23:45 615 查看
现在,DATASNAP倾向于使用JSON作为统一的数据序列格式,以期达到跨平台的效果。于是使用JSON便成为热点。

unituJSONDB;


interface

uses

SysUtils,Classes,Variants,DB,DBClient,SuperObject,Dialogs;

type

TJSONDB=class


private

classfunctiongetJsonFieldNames(res:ISuperObject):TStringList;

classfunctiongetJsonFieldValues(res:ISuperObject):TStringList;

public

classprocedureJsonToClientDataSet(jsonArr:TSuperArray;dstCDS:TClientDataSet);

classfunctionClientDataSetToJSON(srcCDS:TClientDataSet):UTF8String;

end;


implementation


functionGetToken(varastring:string;constfmt:arrayofchar):string;

var

i,j:integer;

Found:Boolean;

begin

found:=false;

result:='';

aString:=TrimLeft(aString);


iflength(astring)=0thenexit;


I:=1;

whileI<=length(Astring)do

begin

found:=false;

ifaString[i]<=#128then

begin

forj:=Low(Fmt)toHigh(Fmt)do

begin

if(astring[i]<>Fmt[j])thencontinue;

found:=true;

break;

end;

ifNotfoundthenI:=I+1;

end

elseI:=I+2;


iffoundthenbreak;

end;


iffoundthen

begin

result:=copy(astring,1,i-1);

delete(astring,1,i);

end

else

begin

result:=astring;

astring:='';

end;

end;


functionGetFieldParams(PropName,Source:string):string;

var

S1,S2:string;

TmpParam:string;

AChar:string;

aValue,aPropName,aSource:string;

begin

Result:='';

ifSource=''thenExit;

aSource:=Source;

whileaSource<>''do

begin

aValue:=GetToken(aSource,[',']);

aPropName:=GetToken(aValue,[':']);

ifCompareText(PropName,aPropName)<>0thencontinue;

Result:=aValue;

break;

end;

end;

//從json取得欄位名稱

classfunctionTJSONDB.getJsonFieldNames(res:ISuperObject):TStringList;

var

i:Integer;

fieldList:TStringList;

fieldNames:String;

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;

finally

//fieldList.Free;

end;

end;


//從json取得欄位值

classfunctionTJSONDB.getJsonFieldValues(res:ISuperObject):TStringList;

var

i:Integer;

fieldList:TStringList;

fieldValues:String;

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;

finally

//fieldList.Free;

end;

end;

//json轉CDS

classprocedureTJSONDB.JsonToClientDataSet(jsonArr:TSuperArray;dstCDS:TClientDataSet);

var

fieldList:TStringList;

valuesList:TStringList;

jsonSrc:string;

i,j:Integer;

begin


fieldList:=getJsonFieldNames(SO[jsonArr[0].AsJson(False,False)]);

if(dstCDS.FieldCount=0)then

begin

fori:=0tofieldList.Count-1do

begin

dstCDS.FieldDefs.Add(fieldList[i],ftString,100,False);

end;

dstCDS.CreateDataSet;

dstCDS.Close;

dstCDS.Open;

end;

try

dstCDS.DisableControls;

fori:=0tojsonArr.Length-1do

begin

jsonSrc:=SO[jsonArr[i].AsJson(False,False)].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;

forj:=0tofieldList.Count-1do

begin

dstCDS.FieldByName(fieldList[j]).AsString:=GetFieldParams(fieldList[j],jsonSrc);

end;

dstCDS.Post;

end;


finally

dstCDS.EnableControls;

end;

end;

//ClientDataSet轉JSON

classfunctionTJSONDB.ClientDataSetToJSON(srcCDS:TClientDataSet):UTF8String;

var

i,j:Integer;

keyValue:String;

jsonList:TStringList;

jsonResult:String;

begin

ifnotsrcCDS.ActivethensrcCDS.Open;


try

jsonList:=TStringList.Create;

srcCDS.DisableControls;

srcCDS.First;

whilenotsrcCDS.Eofdo

begin

keyValue:='';

fori:=0tosrcCDS.FieldDefs.Count-1do

begin

keyValue:=keyValue+Format('"%s":"%s",',[srcCDS.Fields[i].FieldName,srcCDS.Fields[i].AsString]);


end;

jsonList.Add(Format('{%s}',[Copy(keyValue,0,Length(keyValue)-1)]));

srcCDS.Next;

end;

fori:=0tojsonList.Count-1do

begin

jsonResult:=jsonResult+jsonList[i]+',';

end;

Result:=Utf8Encode(Format('[%s]',[Copy(jsonResult,0,Length(jsonResult)-1)]));

finally

srcCDS.EnableControls;

jsonList.Free;

end;

end;




end.

使用範例

//取得資料

[code]procedureTForm1.btnRefreshClick(Sender:TObject);
var

getString:string;

json:ISuperObject;

ja:TSuperArray;

begin

try

getString:=idhtp1.Get('http://localhost/xuan/wsLine.php');

json:=SO(getString);

ja:=json.AsArray;


TJSONDB.JsonToClientDataSet(ja,cdsMain);

finally


end;

end;

//寫入資料

procedureTForm1.btnSubmitClick(Sender:TObject);

var

jsonString:string;

jsonStream:TStringStream;

begin

ifcdsNew.Statein[dsEdit]thencdsNew.Post;

try

jsonString:=TJSONDB.ClientDataSetToJSON(cdsNew);


jsonStream:=TStringStream.Create(jsonString);


idhtp1.HandleRedirects:=True;

idhtp1.ReadTimeout:=5000;

idhtp1.Request.ContentType:='application/json';

idhtp1.Post('http://localhost/xuan/wsLine.php?action=insert',jsonStream);


finally

jsonStream.Free;

end;

end;

[/code]http://www.cnblogs.com/hnxxcxg/archive/2013/02/20/2919813.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: