How to serialize a JObject without the formatting?

假如想象 提交于 2019-12-20 09:25:50

问题


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.

How do I set the formatting to "none" for this?


回答1:


Call JObject's ToString(Formatting.None) method.

Alternatively if you pass the object to the JsonConvert.SerializeObject method it will return the JSON without formatting.

Documentation: Write JSON text with JToken.ToString




回答2:


You can also do the following;

string json = myJObject.ToString(Newtonsoft.Json.Formatting.None);



回答3:


you can use JsonConvert.SerializeObject()

JsonConvert.SerializeObject(myObject) // myObject is returned by JObject.Parse() method

JsonConvert.SerializeObject()

JObject.Parse()



来源:https://stackoverflow.com/questions/570689/how-to-serialize-a-jobject-without-the-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!