Newtonsoft.Json无需转义反斜杠的SerializeObject [英] Newtonsoft.Json SerializeObject without escape backslashes

查看:3333
本文介绍了Newtonsoft.Json无需转义反斜杠的SerializeObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出代码:

dynamic foo = new ExpandoObject();
foo.Bar = "something";
string json = Newtonsoft.Json.JsonConvert.SerializeObject(foo);

输出如下:

"{\"Bar\":\"something\"}"

在调试大型json文档时,很难阅读-使用Newtonsoft.Json的内置功能(不是正则表达式或可能会破坏事情的黑客程序),可以通过以下任何方式使输出字符串:

{Bar: "something"}

解决方案

在查看json值时在调试器中看到的是您应该在C#文件中使用以获得相同值的字符串值.

确实可以替换

dynamic foo = new ExpandoObject();
foo.Bar = "something";
string json = Newtonsoft.Json.JsonConvert.SerializeObject(foo);

string json = "{\"Bar\":\"something\"}";

无需更改程序的行为.

因此,要获取其他值,您应该更改 JSON标准,因此就算了!

如果您实际上没有序列化

The output is below:

"{\"Bar\":\"something\"}"

When debugging a large json document it is hard to read - using the built in features of Newtonsoft.Json (not regex or hacks that could break things) is there any way to make the output a string with the valie:

{Bar: "something"}

What you see in debugger when looking at the json value is the string value that you should use in a C# file to obtain the same value.

Indeed you could replace

dynamic foo = new ExpandoObject();
foo.Bar = "something";
string json = Newtonsoft.Json.JsonConvert.SerializeObject(foo);

with

string json = "{\"Bar\":\"something\"}";

without changing the program's behaviour.

Thus, to obtain a different value, you should change how JsonConvert works, but JsonConvert conforms to the JSON standard, thus forget it!

If you are not actually serializing ExpandoObject (nor any other sealed class out of your control), you can use the DebuggerDisplayAttribute on the types that you are serializing in json, to define how the object will be shown during debug (in your code, the foo instance).

But a string is a string and VisualStudio is right: double-quotes must be escaped.

这篇关于Newtonsoft.Json无需转义反斜杠的SerializeObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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