json 的转义字符串导致 asp.net 服务器端操作

发布于 2024-09-28 00:49:01 字数 451 浏览 0 评论 0原文

我有一个服务器端操作手动生成一些 json 响应。 json 中有一个包含字符串值的属性。

转义此 json 结果中包含的字符串值的最简单方法是什么?

所以这

string result = "{ \"propName\" : '" + (" *** \\\"Hello World!\\\" ***") + "' }";

会变成

string result = "{ \"propName\" : '" + SomeJsonConverter.EscapeString(" *** \\\"Hello World!\\\" ***") + "' }";

并导致以下 json

{ \"propName\" : '*** \"Hello World!\" ***' }

I have a server side operation manually generating some json response. Within the json is a property that contains a string value.

What is the easiest way to escape the string value contained within this json result?

So this

string result = "{ \"propName\" : '" + (" *** \\\"Hello World!\\\" ***") + "' }";

would turn into

string result = "{ \"propName\" : '" + SomeJsonConverter.EscapeString(" *** \\\"Hello World!\\\" ***") + "' }";

and result in the following json

{ \"propName\" : '*** \"Hello World!\" ***' }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终难遇 2024-10-05 00:49:01

首先,我发现手动实现序列化的想法不好。您应该仅出于学习目的或您有其他非常重要的原因不能使用标准 .NET 类(例如必须使用 .NET 1.0-3.0 而不是更高版本)才执行此操作。

现在回到你的代码。您当前生成的结果不是 JSON 格式。您应该将属性名称和属性值放在双配额中:

{ "propName" : "*** \"Hello World!\" ***" }

如何阅读 http://www.json.org/ 双重配额不仅限于必须转义的字符。反斜杠字符也必须转义。您可以在 http://www.jsonlint.com/ 上验证 JSON 结果。

如果您还手动实现反序列化,您应该知道还有更多可以可以转义为 \"\\ 的字符: \/、\b、\f、\n、\r、\t 和 \u 后面是 4 个十六进制数字,

我在答案开头是这样写的,最好使用标准 .NET 类。例如 DataContractJsonSerializerJavaScriptSerializer 如果您必须使用 .NET 2.0 和不更高,您可以使用 Json.NET

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).

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:

{ "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/.

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.

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.

森末i 2024-10-05 00:49:01

你可以尝试这样的事情:

string.replace(/(\\|")/g, "\\$1").replace("\n", "\\n").replace("\r", "\\r");

You may try something like:

string.replace(/(\\|")/g, "\\$1").replace("\n", "\\n").replace("\r", "\\r");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文