- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在将带有转义字符的字符串转换为和从 TJsonString 转换时遇到问题。 (我使用的是 Delphi XE 2、Update 4、Hotfix 1)。
注意:我熟悉 SuperObject ,但我的要求是使用 DBXJSON 单元。
通过 ToString() 方法返回 JSON 表示形式时,TJSONString 似乎未正确转义。
我做错了什么(如果有的话)以及如何正确地将带有特殊字符的字符串转换为正确的 JSON 表示形式?
也许我错过了一些东西,但以下问答似乎都没有直接解决这个问题:
EDIT:
As it turns out, the examples below were indeed working as expected.
What wasn't clear to me was that when creating a TJSONString via it's constructor and adding it to a TJSONObject, the ToString() method will return an escaped representation. However, after parsing a TJSONObject, the ToString() method will returned the un-escaped representation.
The only other caveat was that the EscapeString() function in the sample code below was handling the double-quote. Although I wasn't using the double quote here, some of my other code was, and that caused the parsing to fail because TJSONString already escapes that character. I've updated my sample code to remove this handling from the EscapeString() function, which is what I've been using in my own classes.
Thanks again to @Linas for the answer, which helped me to "get" it.
原始字符串值:
Text := 'c:\path\name' +#13 + #10 + 'Next Line';
Text: c:\path\name
Next Line
DBXJSON 生成什么(无转义):
JsonString: "c:\path\name
Next Line"
JsonPair: "MyString":"c:\path\name
Next Line"
JsonObject: {"MyString":"c:\path\name
Next Line"}
解析非转义文本失败:
Text to parse: {"MyString":"c:\path\name
Next Line"}
Parsed JsonObject = *NIL*
我期望 DBXJSON 生成什么:
Escaped String: c:\\path\\name\r\nNext Line
JsonString: "c:\\path\\name\r\nNext Line"
JsonPair: "MyString":"c:\\path\\name\r\nNext Line"
JsonObject: {"MyString":"c:\\path\\name\r\nNext Line"}
解析转义文本(无效)(要解析的文本已通过 JSONLint 进行验证):
Text to parse: {"MyString":"c:\\path\\name\r\nNext Line"}
Parsed JsonObject.ToString(): {"MyString":"c:\path\name
Next Line"}
我注意到 TJSONString 唯一能正确处理的特殊字符是双引号 (")。
这是我正在使用的代码:
program JsonTest;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, DbxJson;
function EscapeString(const AValue: string): string;
const
ESCAPE = '\';
// QUOTATION_MARK = '"';
REVERSE_SOLIDUS = '\';
SOLIDUS = '/';
BACKSPACE = #8;
FORM_FEED = #12;
NEW_LINE = #10;
CARRIAGE_RETURN = #13;
HORIZONTAL_TAB = #9;
var
AChar: Char;
begin
Result := '';
for AChar in AValue do
begin
case AChar of
// !! Double quote (") is handled by TJSONString
// QUOTATION_MARK: Result := Result + ESCAPE + QUOTATION_MARK;
REVERSE_SOLIDUS: Result := Result + ESCAPE + REVERSE_SOLIDUS;
SOLIDUS: Result := Result + ESCAPE + SOLIDUS;
BACKSPACE: Result := Result + ESCAPE + 'b';
FORM_FEED: Result := Result + ESCAPE + 'f';
NEW_LINE: Result := Result + ESCAPE + 'n';
CARRIAGE_RETURN: Result := Result + ESCAPE + 'r';
HORIZONTAL_TAB: Result := Result + ESCAPE + 't';
else
begin
if (Integer(AChar) < 32) or (Integer(AChar) > 126) then
Result := Result + ESCAPE + 'u' + IntToHex(Integer(AChar), 4)
else
Result := Result + AChar;
end;
end;
end;
end;
procedure Test;
var
Text: string;
JsonString: TJsonString;
JsonPair: TJsonPair;
JsonObject: TJsonObject;
begin
try
Writeln('Raw String Value');
Writeln('-----------------');
Text := 'c:\path\name' +#13 + #10 + 'Next Line';
Writeln('Text: ', Text);
JsonString := TJsonString.Create(Text);
JsonPair := TJsonPair.Create('MyString', JsonString);
JsonObject := TJsonObject.Create(JsonPair);
// DBXJSON results
Writeln;
Writeln('What DBXJSON produces');
Writeln('---------------------');
Writeln('JsonString: ', JsonString.ToString);
Writeln;
Writeln('JsonPair: ', JsonPair.ToString);
Writeln;
Writeln('JsonObject: ', JsonObject.ToString);
Writeln;
// assign JSON representation
Text := JsonObject.ToString;
// free json object
JsonObject.Free;
// parse it
JsonObject:= TJsonObject.ParseJsonValue(TEncoding.ASCII.GetBytes(
Text), 0) as TJsonObject;
Writeln('Parsing UN-escaped Text *FAILS* ');
Writeln('----------------------------------');
Writeln('Text to parse: ', Text);
Writeln;
if (JsonObject = nil) then
Writeln('Parsed JsonObject = *NIL*')
else
Writeln('Parsed JsonObject: ', JsonObject.ToString);
Writeln;
// free json object
JsonObject.Free;
// expected results
Text := 'c:\path\name' +#13 + #10 + 'Next Line';
Text := EscapeString(Text);
JsonString := TJsonString.Create(Text);
JsonPair := TJsonPair.Create('MyString', JsonString);
JsonObject := TJsonObject.Create(JsonPair);
Writeln('What I *EXPECT* DBXJSON to produce');
Writeln('----------------------------------');
Writeln('Escaped String: ', Text);
Writeln;
Writeln('JsonString: ', JsonString.ToString);
Writeln;
Writeln('JsonPair: ', JsonPair.ToString);
Writeln;
Writeln('JsonObject: ', JsonObject.ToString);
Writeln;
// assign JSON representation
Text := JsonObject.ToString;
// free json object
JsonObject.Free;
// parse it
JsonObject:= TJsonObject.ParseJsonValue(TEncoding.ASCII.GetBytes(
Text), 0) as TJsonObject;
Writeln('Parsing ESCAPED Text (*INVALID*) ');
Writeln('----------------------------------');
Writeln('Text to parse: ', Text);
Writeln;
Writeln('Parsed JsonObject.ToString(): ', JsonObject.ToString);
Writeln;
Readln;
except
on E: Exception do
begin
Writeln(E.ClassName, ': ', E.Message);
Readln;
end;
end;
end;
begin
Test;
end.
最佳答案
您可以尝试定义自己的 TJSONString 类型并在那里转义 json 字符串。例如:
uses
DBXJSON;
type
TSvJsonString = class(TJSONString)
private
function EscapeValue(const AValue: string): string;
public
constructor Create(const AValue: string); overload;
end;
{ TSvJsonString }
constructor TSvJsonString.Create(const AValue: string);
begin
inherited Create(EscapeValue(AValue));
end;
function TSvJsonString.EscapeValue(const AValue: string): string;
procedure AddChars(const AChars: string; var Dest: string; var AIndex: Integer); inline;
begin
System.Insert(AChars, Dest, AIndex);
System.Delete(Dest, AIndex + 2, 1);
Inc(AIndex, 2);
end;
procedure AddUnicodeChars(const AChars: string; var Dest: string; var AIndex: Integer); inline;
begin
System.Insert(AChars, Dest, AIndex);
System.Delete(Dest, AIndex + 6, 1);
Inc(AIndex, 6);
end;
var
i, ix: Integer;
AChar: Char;
begin
Result := AValue;
ix := 1;
for i := 1 to System.Length(AValue) do
begin
AChar := AValue[i];
case AChar of
'/', '\', '"':
begin
System.Insert('\', Result, ix);
Inc(ix, 2);
end;
#8: //backspace \b
begin
AddChars('\b', Result, ix);
end;
#9:
begin
AddChars('\t', Result, ix);
end;
#10:
begin
AddChars('\n', Result, ix);
end;
#12:
begin
AddChars('\f', Result, ix);
end;
#13:
begin
AddChars('\r', Result, ix);
end;
#0 .. #7, #11, #14 .. #31:
begin
AddUnicodeChars('\u' + IntToHex(Word(AChar), 4), Result, ix);
end
else
begin
if Word(AChar) > 127 then
begin
AddUnicodeChars('\u' + IntToHex(Word(AChar), 4), Result, ix);
end
else
begin
Inc(ix);
end;
end;
end;
end;
end;
使用示例:
procedure Test;
var
LText, LEscapedText: string;
LJsonString: TSvJsonString;
LJsonPair: TJsonPair;
LJsonObject: TJsonObject;
begin
LText := 'c:\path\name' + #13 + #10 + 'Next Line';
LJsonString := TSvJsonString.Create(LText);
LJsonPair := TJsonPair.Create('MyString', LJsonString);
LJsonObject := TJsonObject.Create(LJsonPair);
try
LEscapedText := LJsonObject.ToString;
//LEscapedText is: c:\\path\\name\r\nNext Line
finally
LJsonObject.Free;
end;
end;
这就是解析的方式:
//AText := '{"MyString":"c:\\path\\name\r\nNext Line"}';
function Parse(const AText: string): string;
var
obj: TJSONValue;
begin
obj := TJSONObject.ParseJSONValue(AText);
try
Result := obj.ToString;
//Result := {"MyString":"c:\path\name
//Next Line"}
finally
obj.Free;
end;
end;
关于json - 如何使用 DBXJSON 将字符串与带有转义/特殊字符的 JSON 相互转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11785963/
我有一个 javascript 从用户输入中读取的 URL。这是 JavaScript 代码的一部分: document.getElementById("Snd_Cont_AddrLnk_BG").v
我将如何在 javascript 中转义斜杠// var j = /^(ht|f)tp(s?)://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$;/ 最佳答案 使用 \ 进行转
在解析到这样的对象之前,我要转义 & 和 =: var obb = parseJSON('{"' + text.replace(/&/g, "\",\"").replace(/=/g,"\":\"")
我正在使用 freemarker 生成一个 freemarker 模板。但我需要一些方法来转义 freemarker 标签。 我将如何逃脱 标签或 ${expression} ? 最佳答案 您也可以使
我正在尝试匹配方括号,即 excel 中正则表达式 VBA 中的 []。我正在尝试使用以下代码,但它不起作用。 Public Function IsSpecial(s As String) As L
我通过设置将 PowerShell 添加到我的上下文菜单中: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\she
我需要转义 $,因此我需要将所有出现的 $ 替换为 \$ 所以我写了这个方法: // String#replaceAll(String regex, String replacement) publi
我正在格式化我的问题。非常遗憾。这是我的问题的摘要 在 JSP 中我有一个字段 我输入的值类似于“cQN==ujyRMdr+Qi8dO9Xm*eRun+ner==aLTyt?aKmGI” 实际行动
我有一个文本文件,其内容是C:\temp 我想要值 C:\temp替换为从变量定义的不同值 此外,将从批处理文件(windows .cmd)中调用 perl oneliner set CMDDIR=C
有没有办法使用 jTemplates 来转义 {$,这样我就可以在 onBlur 中使用内联 javascript,例如 telegraaf 在 processTemplate 之后得到这个: 谢谢
我正在尝试将 wget 与包含“#”符号的 url 一起使用。无论我做什么来逃避这个角色,它都不起作用。我用过\、' 和 "。但它们都不起作用。有人有什么建议吗? 谢谢! 最佳答案 如果您真的想让它有
我想知道如何从数据库中回显带有 $ 符号的字符串。此时,数据库中的值“Buy one for $5.00”将转换为“Buy one for .00”。 假设该字段的名称为 title,值为 Buy o
我在 mySQL 中有一个查询,旨在返回我们网站上使用的搜索词。是的,这是一个标签云,是的,我知道它是一条鲻鱼 :) 我们有一个管理页面,管理员可以在其中查看搜索词并选择将它们排除在云端之外。这些词进
我有一个文本区域。在其点击事件上。我将其插入数据库中,然后将其显示为元素列表中的第一个元素。问题是。如果我输入""在textarea中,jquery无法正确显示。它显示为空。代码是 var note
我想知道是否有某种字符串前缀,这样 cstring 就可以按原样使用,而不需要我转义所有字符。我不是 100% 确定。我记得一些关于在字符串前加上 @ 符号( char str[] = @"some\
这个问题在这里已经有了答案: How do I escape curly-brace ({}) characters in a string while using .format (or an f
C/C++编译器如何操作源代码中的转义字符["\"]?如何编写用于处理该字符的编译器语法?遇到那个字符后,编译器会做什么? 最佳答案 大多数编译器分为几个部分:编译器前端称为 lexical anal
我计划接受用户输入,并将其插入到一个 div 中 user_content 一个用户提供内容,另一个用户接收内容。 我认为我会遵循的建议来自 https://www.owasp.org/index.p
我有一个这种形式的 url - http:\\/\\/en.wikipedia.org\\/wiki\\/The_Truman_Show。我怎样才能使它成为正常的网址。我试过使用 urllib.unq
我有一个带有转义数据的字符串 escaped_data = '\\x50\\x51' print escaped_data # gives '\x50\x51' 什么 Python 函数会对其进行反转
我是一名优秀的程序员,十分优秀!