问题描述
我如何解析JSON字符串包含特殊字符中的一个值?
How do I parse JSON string with one of the values containing special characters?
JObject obj = JObject.Parse(str);
STR值:
{
"message": "some !@#$%^&*(){}:"?/?/|"':>;><{"d":"v"}"
}
我有execption:解析遇到一个意外的字符值后:{的
推荐答案
这JSON是无效的。如果一个JSON字符串中包含特殊字符,如双引号,反斜线
\\
或斜线 /
,他们需要用反斜杠进行转义 \\
。(见的)无JSON解析器,包括Json.Net,将能够处理与格式不正确摆在首位一个JSON字符串。
That JSON is invalid. If a JSON string contains special characters like double quotes "
, backslashes \
or slashes /
, they need to be escaped with backslashes \
. (See JSON.org.) No JSON parser, including Json.Net, will be able to deal with a JSON string that isn't properly formatted in the first place.
您JSON需要像这样才能够正确解析:
Your JSON would need to look like this to be able to be parsed correctly:
{
"message": "some !@#$%^&*(){}:\"?/?/|\"':>;><{\"d\":\"v\"}"
}
的解决方案是在源以正确序列的串
The solution is to correctly serialize the string at the source.
这篇关于JSON.Net反序列化字符串,它包含特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!