Newtonsoft.Json无需转义反斜杠的SerializeObject [英] Newtonsoft.Json SerializeObject without escape backslashes
问题描述
给出代码:
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标准,因此就算了!
如果您实际上没有序列化 DebuggerDisplayAttribute (在您要在json中序列化的类型上),以定义在调试过程中对象的显示方式(在您的代码中,为foo
实例).
但是字符串是字符串,VisualStudio是正确的:必须对双引号进行转义.
Given the code:
dynamic foo = new ExpandoObject();
foo.Bar = "something";
string json = Newtonsoft.Json.JsonConvert.SerializeObject(foo);
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屋!