jSon_encode就像Delphi接受TDataSet的函数一样 [英] jSon_encode like function for Delphi which accepts TDataSet

查看:136
本文介绍了jSon_encode就像Delphi接受TDataSet的函数一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是在Delphi 2007中创建Indy服务器,与客户端进行通信,并从基于Sql的数据库返回json格式的数据。我们办公室的某个人使用php创建了一个原型。而在原型中,它们广泛地使用 jSon_encode 函数返回表中的数据。我想知道是否有类似的Delphi函数可以接受 TDataSet 参数并返回正确格式化的json数据。



任何人都知道这样的功能?



更新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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆