简体   繁体   English

JSON \\ n中的特殊字符和双引号

[英]special character in JSON \n and double quote

I am having problem when convert string to json object. 将字符串转换为json对象时出现问题。

\\n gives out Unterminated string . \\n给出Unterminated string If I want to use new line I use \\n character, then it occurs the error. 如果我想使用换行符,我使用\\ n字符,那么它将发生错误。 Example String: Hello \\n this is testing message. 示例字符串:您好\\ n这是测试消息。

double quote " gives out Expected a ',' or '} Exception. Example String : This is a "TESTING" message. 双引号“给出了Expected a ',' or '}异常。示例字符串:这是一条” TESTING“消息。

I use to escape \\ to escape those , but still facing the JSON Exception 我过去使用\\来逃避\\,但是仍然面对JSON异常

The \\n, intended as "newline", must be encoded as \\n in json. \\ n,意为“换行符”,必须在json中编码为\\ n。 \\" is for quoting. \\”用于引用。

You can open the python shell, write 您可以打开python shell,编写

>>> import json
>>> json.dumps('\n"')

and see the result yourself 自己看结果

I use the StringEscapeUtils.escapeJavaScript() function which make my life easy. 我使用StringEscapeUtils.escapeJavaScript()函数使我的生活变得轻松。 http://commons.apache.org/lang/api/org/apache/commons/lang3/StringEscapeUtils.html http://commons.apache.org/lang/api/org/apache/commons/lang3/StringEscapeUtils.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM