jSon_encode like function for Delphi which accepts TDataSet

安稳与你 提交于 2019-12-30 10:12:13

问题


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

回答1:


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



回答2:


We just added a more complete and faster function, in our Open Source repository.

It is part of our mORMot framework, but can be used as a stand-alone unit, not tied to other features.

See in SynVirtualDataSet.pas:

function DataSetToJSON(Data: TDataSet): RawUTF8 

See this commit and the associated forum thread.




回答3:


You can change every row into object and use serializing http://docwiki.embarcadero.com/RADStudio/XE5/en/Serializing_User_Objects



来源:https://stackoverflow.com/questions/20481410/json-encode-like-function-for-delphi-which-accepts-tdataset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!