jSon_encode就像Delphi接受TDataSet的函数一样 [英] jSon_encode like function for Delphi which accepts TDataSet
问题描述
任何人都知道这样的功能?
更新12/10/2013 - 我修改为@ user2748835答案:
函数jsonencode(mString:String):String;
begin
result:= StringReplace(mString,''','\''',[rfReplaceAll,rfIgnoreCase]);
result:= StringReplace(mString,'\','\\',[rfReplaceAll,rfIgnoreCase]);
result:= StringReplace(result,crlf,'\\\
',[rfReplaceAll,rfIgnoreCase]);
result:= StringReplace(result,'','\',[rfReplaceAll,rfIgnoreCase]);
result:= StringReplace(result,'/','\ /',[rfReplaceAll,rfIgnoreCase]);
result:= StringReplace(result,'#9','\t',[rfReplaceAll,rfIgnoreCase]);
结束
函数jSon_encode(aDataset:TDataset):string;
函数fieldToJSON(thisField:TField):string;
begin
try
result:=''+ thisField.fieldName +':';
case thisField.DataType
ftInteger,ftSmallint,ftLargeint:
result:= result + inttostr(thisField.AsInteger);
ftDateTime:
result:= result +''+ formatdatetime('YYYY-MM-DD HH:NN:SS',thisField.AsDateTime)+'';
ftCurrency,
ftFloat:
result:= result + floattostr(thisField.AsFloat);
ftString:
result:= result +''+ jsonencode(thisField.AsString)+'';
else
end; // case
result:= result +',';
除了
对于e:异常做开始
appendtolog('问题转义字段'+ thisfield.fieldname);
结束
结束
end; // of fieldToJSON
函数rowToJSON(ds:TDataset):string;
var
fieldIx:integer;
begin
result:='';
for fieldIx:= 0 to ds.fieldcount-1 do
result:= result + fieldToJSON(ds.Fields [fieldIx]);
//最后一个col
结尾的修剪逗号结果:='{'+ copy(result,1,length(result)-1)+'},';
结束// of rowToJSON
begin
result:='';
与aDataset do
begin
如果不是bof然后第一;
而不是eof做
begin
result:= result + rowToJSON(aDataset);
next;
结束
结束
//如果长度(结果)> 0,然后
结果:= copy(result,1,length(result)-1),最后添加一个逗号并添加
;
result:='['+ result +']';
结束// DSToJSON
在TDataset中,您可以循环通过Fields集合并构造json输出,然后在循环中,检查fieldtype并相应地对该值进行编码。
这样的东西:
使用db;
函数DSToJSON(aDataset:TDataset):string;
函数fieldToJSON(thisField:TField):string;
begin
result:=''+ thisField.fieldName +':';
case thisField.DataType
ftInteger,
ftSmallint,
ftCurrency,
ftFloat,
ftLargeInt:
result:= result + thisField。值+ ^ n ^ j;
ftString:
result:= noSingleQuotes(thisField.value)+ ^ n ^ j;
else
end; // case
end; // of fieldToJSON
function rowToJSON(ds:TDataset):string;
var
fieldIx:integer;
begin
for fieldIx:= 0 to ds.fieldcount-1 do
result:= result + fieldToJSON(ds.Fields [fieldIx]);
//最后一个col
结尾的修剪逗号结果:='{'+ copy(result,1,length(result)-1)+'},';
结束// of rowToJSON
begin
result:='';
与aDataset do
begin
如果不是bof然后第一;
而不是eof做
begin
result:= result + rowToJSON(aDataset);
next;
结束
结束
//如果长度(结果)> 0,然后
结果:= copy(result,1,length(result)-1),最后添加一个逗号并添加
;
result:='['+ result +']';
结束// DSToJSON
I have been tasked with creating a Indy server in Delphi 2007 which communicates with clients and returns json formatted data from Sql based databases. Someone from our office created a prototype using php. And in the prototype they use the jSon_encode function extensively to return the data from tables. I was wondering if there was a similar Delphi function which could accept a TDataSet parameter and return properly formatted json data.
Anyone know of such function?
Update 12/10/2013 - my modification to @user2748835 answer:
function jsonencode(mString: String): String;
begin
result := StringReplace(mString,'''','\''',[rfReplaceAll,rfIgnoreCase]);
result := StringReplace(mString,'\','\\',[rfReplaceAll,rfIgnoreCase]);
result := StringReplace(result,crlf,'\n',[rfReplaceAll,rfIgnoreCase]);
result := StringReplace(result,'"','\"',[rfReplaceAll,rfIgnoreCase]);
result := StringReplace(result,'/','\/',[rfReplaceAll,rfIgnoreCase]);
result := StringReplace(result,'#9','\t',[rfReplaceAll,rfIgnoreCase]);
end;
function jSon_encode(aDataset:TDataset):string;
function fieldToJSON(thisField:TField):string;
begin
try
result := '"'+thisField.fieldName+'":';
case thisField.DataType of
ftInteger,ftSmallint,ftLargeint:
result := result+inttostr(thisField.AsInteger);
ftDateTime:
result := result+'"'+formatdatetime('YYYY-MM-DD HH:NN:SS',thisField.AsDateTime)+'"';
ftCurrency,
ftFloat:
result := result + floattostr(thisField.AsFloat);
ftString :
result := result + '"'+jsonencode(thisField.AsString)+'"';
else
end; // case
result := result + ',';
except
on e: Exception do begin
appendtolog('problem escaping field '+thisfield.fieldname);
end;
end;
end; // of fieldToJSON
function rowToJSON(ds:TDataset):string;
var
fieldIx : integer;
begin
result := '';
for fieldIx := 0 to ds.fieldcount-1 do
result := result + fieldToJSON(ds.Fields[fieldIx]);
// trim comma after last col
result := '{'+copy(result,1,length(result)-1)+'},';
end; // of rowToJSON
begin
result := '';
with aDataset do
begin
if not bof then first;
while not eof do
begin
result := result + rowToJSON(aDataset);
next;
end;
end;
//strip last comma and add
if length(result)>0 then
result := copy(result,1,length(result)-1);
result := '['+result+']';
end; // of DSToJSON
In a TDataset, you can loop through the Fields collection and construct the json output and then in the loop, check the fieldtype and encode the value accordingly. Something like:
uses db;
function DSToJSON(aDataset:TDataset):string;
function fieldToJSON(thisField:TField):string;
begin
result := '"'+thisField.fieldName+'":';
case thisField.DataType of
ftInteger,
ftSmallint,
ftCurrency,
ftFloat,
ftLargeInt:
result := result+thisField.value+^n^j;
ftString :
result := noSingleQuotes(thisField.value)+^n^j;
else
end; // case
end; // of fieldToJSON
function rowToJSON(ds:TDataset):string;
var
fieldIx : integer;
begin
for fieldIx := 0 to ds.fieldcount-1 do
result := result + fieldToJSON(ds.Fields[fieldIx]);
// trim comma after last col
result := '{'+copy(result,1,length(result)-1)+'},';
end; // of rowToJSON
begin
result := '';
with aDataset do
begin
if not bof then first;
while not eof do
begin
result := result + rowToJSON(aDataset);
next;
end;
end;
//strip last comma and add
if length(result)>0 then
result := copy(result,1,length(result)-1);
result := '['+result+']';
end; // of DSToJSON
这篇关于jSon_encode就像Delphi接受TDataSet的函数一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!