问题描述
给定代码:
dynamic foo = new ExpandoObject();
foo.Bar = "something";
string json = Newtonsoft.Json.JsonConvert.SerializeObject(foo);
输出如下:
"{"Bar":"something"}"
在调试大型 json 文档时,很难阅读 - 使用 Newtonsoft.Json 的内置功能(不是可能破坏事物的正则表达式或黑客)有没有办法使输出成为带有 valie 的字符串:
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"}
推荐答案
您在调试器中查看 json
值时看到的是字符串值,您应该在 C# 文件中使用它来获取相同的值.
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.
确实可以替换
dynamic foo = new ExpandoObject();
foo.Bar = "something";
string json = Newtonsoft.Json.JsonConvert.SerializeObject(foo);
与
string json = "{"Bar":"something"}";
不改变程序的行为.
因此,要获得不同的值,您应该更改 JsonConvert 有效,但是 JsonConvert 符合 JSON 标准,所以算了!
Thus, to obtain a different value, you should change how JsonConvert works, but JsonConvert conforms to the JSON standard, thus forget it!
如果您实际上没有序列化 ExpandoObject(或任何其他不受您控制的密封类),您可以使用 DebuggerDisplayAttribute 关于您在 json 中序列化的类型,以定义在调试期间对象的显示方式(在您的代码中,foo
实例).
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).
但字符串就是字符串,VisualStudio 是对的:双引号必须转义.
But a string is a string and VisualStudio is right: double-quotes must be escaped.
这篇关于没有转义反斜杠的 Newtonsoft.Json SerializeObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!