Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

c# - Serialize an object directly to a JObject instead of to a string in json.net

How might one serialize an object directly to a JObject instance in JSON.Net? What is typically done is to convert the object directly to a json string like so:

string jsonSTRINGResult = JsonConvert.SerializeObject(someObj);

One could then deserialize that back to a JObject as follows:

JObject jObj = JsonConvert.DeserializeObject<JObject>(jsonSTRINGResult);

That seems to work, but it would seem like this way has a double performance hit (serialize and then deserialize). Does SerializeObject internally use a JObject that can be accessed somehow? Or is there some way to just serialize directly to a JObject?

question from:https://stackoverflow.com/questions/33088297/serialize-an-object-directly-to-a-jobject-instead-of-to-a-string-in-json-net

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use FromObject static method of JObject

JObject jObj = JObject.FromObject(someObj)

http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JObject_FromObject.htm


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...