I have a server side operation manually generating some json response. Within the json is a property that contains a string value.
我有一个服务器端操作手动生成一些json响应。在json中是一个包含字符串值的属性。
What is the easiest way to escape the string value contained within this json result?
转义此json结果中包含的字符串值的最简单方法是什么?
So this
string result = "{ \"propName\" : '" + (" *** \\\"Hello World!\\\" ***") + "' }";
would turn into
会变成
string result = "{ \"propName\" : '" + SomeJsonConverter.EscapeString(" *** \\\"Hello World!\\\" ***") + "' }";
and result in the following json
并导致以下json
{ \"propName\" : '*** \"Hello World!\" ***' }
2 个解决方案
#1
1
First of all I find the idea to implement serialization manually not good. You should to do this mostla only for studying purpose or of you have other very important reason why you can not use standard .NET classes (for example use have to use .NET 1.0-3.0 and not higher).
首先,我发现手动实现序列化的想法并不好。你应该只为了学习目的而这样做,或者你有其他非常重要的原因,你不能使用标准的.NET类(例如使用必须使用.NET 1.0-3.0而不是更高)。
Now back to your code. The results which you produce currently are not in JSON format. You should place the property name and property value in double quotas:
现在回到你的代码。您当前生成的结果不是JSON格式。您应该将属性名称和属性值放在双配额中:
{ "propName" : "*** \"Hello World!\" ***" }
How you can read on http://www.json.org/ the double quota in not only character which must be escaped. The backslash character also must be escaped. You cen verify you JSON results on http://www.jsonlint.com/.
您如何在http://www.json.org/上阅读不仅必须转义的字符的双重配额。反斜杠字符也必须转义。您可以在http://www.jsonlint.com/上验证您的JSON结果。
If you implement deserialization also manually you should know that there are more characters which can be escaped abbitionally to \"
and \\
: \/
, \b, \f, \n, \r, \t and \u which follows to 4 hexadecimal digits.
如果你手动实现反序列化,你应该知道还有更多的字符可以被转义到\“和\\:\ /,\ b,\ f,\ n,\ r,\ t和\ u,后面跟4到4十六进制数字。
How I wrote at the beginning of my answer, it is better to use standard .NET classes like DataContractJsonSerializer or JavaScriptSerializer. If you have to use .NET 2.0 and not higher you can use Json.NET.
我在回答的开头写的,最好使用标准的.NET类,如DataContractJsonSerializer或JavaScriptSerializer。如果必须使用.NET 2.0而不是更高版本,则可以使用Json.NET。
#2
0
You may try something like:
您可以尝试以下方式:
string.replace(/(\\|")/g, "\\$1").replace("\n", "\\n").replace("\r", "\\r");
#1
1
First of all I find the idea to implement serialization manually not good. You should to do this mostla only for studying purpose or of you have other very important reason why you can not use standard .NET classes (for example use have to use .NET 1.0-3.0 and not higher).
首先,我发现手动实现序列化的想法并不好。你应该只为了学习目的而这样做,或者你有其他非常重要的原因,你不能使用标准的.NET类(例如使用必须使用.NET 1.0-3.0而不是更高)。
Now back to your code. The results which you produce currently are not in JSON format. You should place the property name and property value in double quotas:
现在回到你的代码。您当前生成的结果不是JSON格式。您应该将属性名称和属性值放在双配额中:
{ "propName" : "*** \"Hello World!\" ***" }
How you can read on http://www.json.org/ the double quota in not only character which must be escaped. The backslash character also must be escaped. You cen verify you JSON results on http://www.jsonlint.com/.
您如何在http://www.json.org/上阅读不仅必须转义的字符的双重配额。反斜杠字符也必须转义。您可以在http://www.jsonlint.com/上验证您的JSON结果。
If you implement deserialization also manually you should know that there are more characters which can be escaped abbitionally to \"
and \\
: \/
, \b, \f, \n, \r, \t and \u which follows to 4 hexadecimal digits.
如果你手动实现反序列化,你应该知道还有更多的字符可以被转义到\“和\\:\ /,\ b,\ f,\ n,\ r,\ t和\ u,后面跟4到4十六进制数字。
How I wrote at the beginning of my answer, it is better to use standard .NET classes like DataContractJsonSerializer or JavaScriptSerializer. If you have to use .NET 2.0 and not higher you can use Json.NET.
我在回答的开头写的,最好使用标准的.NET类,如DataContractJsonSerializer或JavaScriptSerializer。如果必须使用.NET 2.0而不是更高版本,则可以使用Json.NET。
#2
0
You may try something like:
您可以尝试以下方式:
string.replace(/(\\|")/g, "\\$1").replace("\n", "\\n").replace("\r", "\\r");