C# Json.NET 在返回 json 序列化字符串时添加反斜杠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18225921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Json.NET adding backslash while returning json serialized string
提问by Soham Dasgupta
I am trying to serialize a list to json string using Json.NET but the return string has backslash within it, which in turn is failing a json parsing.
我正在尝试使用 Json.NET 将列表序列化为 json 字符串,但返回字符串中包含反斜杠,这反过来导致 json 解析失败。
var x = from d in entities.Books.ToList()
select new
{
ID = d.ID,
BookName = d.BookName
};
return JsonConvert.SerializeObject(x.ToList());
The above code returns
上面的代码返回
"[{\"ID\":1,\"BookName\":\"MVC Music Store - Tutorial - v3.0\"},{\"ID\":2,\"BookName\":\"Pro.ASP.NET.MVC.3.Framework\"},{\"ID\":3,\"BookName\":\"Application Architecture Guide v2\"},{\"ID\":4,\"BookName\":\"Gang of Four Design Patterns\"},{\"ID\":5,\"BookName\":\"CS4 Pocket Reference\"}]"
which fails all JSON parsing. How can I remove these.
所有 JSON 解析失败。我怎样才能删除这些。
采纳答案by xanatos
No. it doesn't
不,它没有
class Program
{
class Book
{
public int ID;
public string BookName;
}
static void Main()
{
var books = new List<Book> { new Book { ID = 1, BookName = "A" }, new Book { ID = 2, BookName = "B" } };
var x = from d in books
select new
{
ID = d.ID,
BookName = d.BookName
};
string str = JsonConvert.SerializeObject(x.ToList());
Console.WriteLine(str);
}
}
There could be two problems:
可能有两个问题:
A) You are looking at the result from the debugger. To check for this, Put the JsonConvert
in a temporary variable (like I did) and look at it with the debugger. Click on the arrow right of the hourglass and select Text Visualizer
.
A) 您正在查看调试器的结果。要检查这一点,将 放入JsonConvert
一个临时变量(就像我所做的那样)并使用调试器查看它。单击沙漏右侧的箭头并选择Text Visualizer
。
or
或者
B) The calling method is transforming the object againto Json, so escaping everything.
B) 调用方法再次将对象转换为 Json,因此逃避了一切。
回答by Damini Suthar
string str = "Your string with slashes";
str = JToken.Parse({your string here}).ToString();
回答by Hassan Shouman
The JSON object is serialized twice.
JSON 对象被序列化两次。
I solved by:
我解决了:
Declaring the operation contract of the method response format to return JSON. I changed the method to return an object instead of a string.
声明方法响应格式的操作契约返回JSON。我改变了方法来返回一个对象而不是一个字符串。
The serializing of Jason will be done automatically behind the scenes.
Jason 的序列化将在幕后自动完成。
回答by vezenkov
I was getting the same result, but with doubled escape shashes while I was unit testing some json serialization. Looking at my code I realized I am serializing the "expected" json string instead of the actual .net object. So, passing a json string to JsonConvert.SerializeObject(expectedJsonString)
will simply escape it once over. This is how I came here, and this is the answer I wrote, when I realized I just did a coding mistake... Did you just realize yours?
我得到了相同的结果,但是当我对一些 json 序列化进行单元测试时,转义字符增加了一倍。查看我的代码,我意识到我正在序列化“预期的”json 字符串而不是实际的 .net 对象。因此,将 json 字符串传递给JsonConvert.SerializeObject(expectedJsonString)
将简单地将其转义一次。这就是我来到这里的方式,这就是我写的答案,当我意识到我只是犯了一个编码错误......你刚刚意识到你的吗?