0

我正在创建一个为 cakephp 提供 json 的服务器数据快照。我为服务器发送 cakephp 的 sql ( ....Methods1/GetData/select ID , NAME , PROPRIEDADE_ID , AREA_TOTAL , AREA_UTIL , MARKER , COLOR , OBS, LOCATION , TYPE 字段 from where PROPRIEDADE_ID = 333 )

在服务器数据快照中:

function TServerMethods1.GetData(sql:string): string;
var  jsonObj, jSubObj: TJSONObject;
ja: TJSONArray;  jp, jSubPar: TJSONPair;  i, j: integer;
begin

           with Form1 do
              begin
               Form1.Memo1.clear;
                      SQLDataSet1.Close;
                      SQLDataSet1.CommandText:=sql;
                      SQLDataSet1.Open;

                  GetInvocationMetadata().ResponseContent:=DataSetToJsonTxt(SQLDataSet1); // Function bellow 
                  Form1.Memo1.Lines.Text:=DataSetToJsonTxt(SQLDataSet1);
                  GetInvocationMetadata().ResponseCode := 200;
                  Result :=GetInvocationMetadata().ResponseContent;


        end;
end;

// convert dataset em Json and result a string
function TServerMethods1.DataSetToJsonTxt(pDataSet: TDataSet): string;
var
    ArrayJSon:TJSONArray;
    ObjJSon:TJSONObject;
    strJSon:TJSONString;
    intJSon:TJSONNumber;
    TrueJSon:TJSONTrue;
    FalseJSon:TJSONFalse;
    I: Integer;
    pField:TField;
begin
  ArrayJSon:=TJSONArray.Create;
    try
      pDataSet.First;
        while not pDataSet.Eof do
          begin
            ObjJSon:=TJSONObject.Create;
            for pField in pDataSet.Fields do
            case pField.DataType of
              ftString:
                begin
                  strJSon:=TJSONString.Create( stringReplace(pField.AsString, #13#10, '', [rfReplaceAll]));
                  ObjJSon.AddPair(pField.FieldName,strJSon);
                end;
             ftInteger:
                 begin
                  IntJSon:=TJSONNumber.Create(pField.AsInteger);
                  ObjJSon.AddPair(pField.FieldName,IntJSon);
                end;
      else //casos gerais são tratados como string
        begin
          strJSon:=TJSONString.Create(pField.AsString);
          ObjJSon.AddPair(pField.FieldName,strJSon);
       end;
  end;
    ArrayJSon.AddElement(ObjJSon);
    pDataSet.next;
end;
result:=ArrayJSon.ToString;
finally
   ArrayJSon.Free;
end;
end;

我的结果是:

[
    {
        "ID": 159,
        "NOME": "1",`enter code here`
        "PROPRIEDADE_ID": 333,
        "AREA_TOTAL": "22154,53",
        "AREA_UTIL": "22154,53",
        "MARCADOR": "-30.1646804809571,-55.6715869903565",
        "COR": "5E8FAD",
        "OBS": "",
        "LOCALIZACAO": "(-30.1646804809571,-55.6715869903565)(-30.1647877693177,-55.6723594665528)(-30.1645088195801,-55.6726598739625)(-30.1643586158753,-55.6731534004212)(-30.1645946502686,-55.6734323501587)(-30.1655817031861,-55.6736898422242)(-30.1665043830872,-55.6741404533387)(-30.1664614677429,-55.6748700141907)(-30.1662039756775,-55.6749343872071)(-30.1655387878418,-55.6742477416993)(-30.1636505126953,-55.6734108924866)(-30.1636934280396,-55.6730890274048)(-30.164122581482,-55.6728100776673)(-30.1644444465637,-55.6723594665528)(-30.1646804809571,-55.6715869903565)",
        **"MODALIDADE": "ás~dçasArrendada"**
    }
]

如果我在我的 sql 中包含 MODALIDADE 字段,则 cakephp 无法获取数据。问题是字符 (ás~dçasArrendada)long 和字段 LOCALIZACAO 中的换行符。有什么方法可以格式化结果 json 对象吗?

4

1 回答 1

0

我的解决方案:

          ftString:
            begin
                    texto:=stringReplace(pField.AsString, #13#10, ' ', [rfReplaceAll]);
                    texto:=UTF8EncodeToShortString(texto);
                    ObjJSon.ParseJSONValue(BytesOf(texto),0);
                    strJSon:=TJSONString.Create(texto);
                    ObjJSon.AddPair(pField.FieldName,strJSon);


            end;
于 2013-11-09T22:11:20.577 回答