Newtonsoft.Json SerializeObject without escape backslashes(没有转义反斜杠的 Newtonsoft.Json SerializeObject)
问题描述
给定代码:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:没有转义反斜杠的 Newtonsoft.Json SerializeObject


基础教程推荐
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01