json 如何在没有格式的情况下序列化 JObject?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/570689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to serialize a JObject without the formatting?
提问by Hugo
I have a JObject(I'm using Json.Net) that I constructed with LINQ to JSON (also provided by the same library). When I call the ToString()method on the JObject, it outputs the results as formatted JSON.
我有一个JObject(我正在使用 Json.Net),我用 LINQ to JSON(也由同一个库提供)构建了它。当我调用 上的ToString()方法时JObject,它会将结果输出为格式化的 JSON。
How do I set the formatting to "none" for this?
如何为此将格式设置为“无”?
回答by James Newton-King
Call JObject's ToString(Formatting.None)method.
调用 JObject 的ToString(Formatting.None)方法。
Alternatively if you pass the object to the JsonConvert.SerializeObject method it will return the JSON without formatting.
或者,如果您将对象传递给 JsonConvert.SerializeObject 方法,它将返回不带格式的 JSON。
Documentation: Write JSON text with JToken.ToString
回答by Simpu
You can also do the following;
您还可以执行以下操作;
string json = myJObject.ToString(Newtonsoft.Json.Formatting.None);
回答by Mawardy
you can use JsonConvert.SerializeObject()
您可以使用 JsonConvert.SerializeObject()
JsonConvert.SerializeObject(myObject) // myObject is returned by JObject.Parse() method

