没有转义反斜杠的 Newtonsoft.Json SerializeObject

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)