.NET 4.0 JSON序列:双引号更改为\\" [英] .Net 4.0 JSON Serialization: Double quotes are changed to \"

查看:171
本文介绍了.NET 4.0 JSON序列:双引号更改为\\"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用System.Web.Script.Serialization.JavaScriptSerializer()来序列字典对象成JSON字符串。我需要发送此JSON字符串API坐在云。然而,当我们把它序列化,序列化替换所有以\\双引号

例如 -

理想json_string = {K:JSON,数据:yeehaw}

串行搞砸json_string = {\\K \\:\\JSON \\,\\DATA \\:\\yeehaw \\}

任何想法,为什么它这样做?而且我还用外部包像json.net但仍不能解决问题。

code -

 词典<字符串,字符串> json_value =新词典<字符串,字符串>();
json_value.Add(K,JSON);
json_value.Add(数据,yeehaw);
VAR jsonSerializer =新System.Web.Script.Serialization.JavaScriptSerializer();
字符串json_string = jsonSerializer.Serialize(json_value);


解决方案

我要大胆地说,你正在寻找在断点IDE中的猜测。在这种情况下,有在这里是没有问题的。你们看到的是完全有效的JSON;只需将IDE使用转义字符串表示法来显示给你。在内容字符串,但是,是你的理想的字符串。它使用各种原因转义版本:


  • ,这样就可以正确地看到并确定非文本字符,如标签,回车,换行等

  • 这样,有很多新行的字符串可以显示在一个基于横向视图

  • ,以便它可以明确的是,它是一个字符串,即富与\\中的报价(外侧引号告诉你,这是一个字符串;如果内报价并没有逃脱它将会引起混乱)

  • 让你可以复制/粘贴值到编辑器或立即窗口(等),而无需添加转义自己

I'm using System.Web.Script.Serialization.JavaScriptSerializer() to serialize dictionary object into JSON string. I need to send this JSON string to API sitting in the cloud. However, when we serialize it, serializer replaces all the double quotes with \"

For example -

Ideal json_string = {"k":"json", "data":"yeehaw"}

Serializer messed up json_string = {\"k\":\"json\",\"data\":\"yeehaw\" }

Any idea why it is doing so? And I also used external packages like json.net but it still doesn't fix the issues.

Code -

Dictionary<string, string> json_value = new Dictionary<string, string>();
json_value.Add("k", "json");
json_value.Add("data", "yeehaw");
var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json_string = jsonSerializer.Serialize(json_value);

解决方案

I'm going to hazard the guess that you're looking in the IDE at a breakpoint. In which case, there is no problem here. What you are seeing is perfectly valid JSON; simply the IDE is using the escaped string notation to display it to you. The contents of the string, however, are your "ideal" string. It uses the escaped version for various reasons:

  • so that you can correctly see and identify non-text characters like tab, carriage-return, new-line, etc
  • so that strings with lots of newlines can be displayed in a horizontal-based view
  • so that it can be clear that it is a string, i.e. "foo with \" a quote in" (the outer-quotes tell you it is a string; if the inner quote wasn't escaped it would be confusing)
  • so that you can copy/paste the value into the editor or immediate-window (etc) without having to add escaping yourself

这篇关于.NET 4.0 JSON序列:双引号更改为\\&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆