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]
相关文章推荐
- jstl param url redirect import
- js没有strstr, 而应该用indexOf函数
- jstl fortokens 分割字符串
- JavaScript的祖师爷-----布兰登艾奇
- jsp指令标识
- js函数收藏:获取cookie值
- 【JavaScript DOM 编程艺术】 笔记
- JS-表单提交检查表单字数方法
- js中的串替换函数-------顺便说说如何去掉字符串的所有空格
- jstl foreach标签
- jsp隐式对象
- jstl catch if choose标签
- JS-实时修改在textarea里面的span(实际输入的文字)
- 去除JSP页面自动生成的空行
- ajax&json实现二级下拉框联动,简单示例
- js引用类型
- js面向对象--创建对象
- js 判断两个变量是否相等
- jstl core 库 之 out set remove
- javascript 调用C++函数