How do I convert a string to and from JSON with escaped/special characters using DBXJSON?

前端 未结 3 1957
长发绾君心
长发绾君心 2020-12-28 08:54

I\'m having trouble converting a string with escaped characters to and from a TJsonString. (I\'m using Delphi XE 2, Update 4, Hotfix 1).

3条回答
  •  礼貌的吻别
    2020-12-28 08:59

    I ran into the same problem with Delphi XE2. My solution was to alter the library file Data.DBXJSON.pas. I copied the original file to my PatchedLibraryXE2 directory (who's first in my library path). Here I changed the method

    function TJSONString.ToString: UnicodeString;
    begin
      if FStrBuffer <> nil then
    //    Exit('"' + AnsiReplaceStr(FStrBuffer.ToString, '"', '\"') + '"');
        Exit('"' + EscapeValue(FStrBuffer.ToString) + '"');
      Result := NullString;
    end;
    

    I used the escape method of Linas in his answer above. I left out the escape of characters bigger than 127. To compile I also needed to copy the file Data.DBXCommon.pas to my PatchedLibraryXE2 directory. Now my TJSONObject.ToString() works as expected.

提交回复
热议问题