问题描述
如何将对象直接序列化为JSON.Net中的JObject
实例?通常要做的是将对象直接转换为json string ,如下所示:
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);
然后可以反序列化,将其返回到JObject
,如下所示:
One could then deserialize that back to a JObject
as follows:
JObject jObj = JsonConvert.DeserializeObject<JObject>(jsonSTRINGResult);
这似乎可行,但是这种方式似乎对性能有双重影响(先序列化然后反序列化). SerializeObject
在内部是否使用可以通过某种方式访问的JObject
?还是有某种方法可以直接直接序列化为JObject
?
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
?
推荐答案
您可以使用FromObject
的FromObject
静态方法
JObject jObj = JObject.FromObject(someObj)
http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JObject_FromObject.htm
这篇关于将对象直接序列化为JObject而不是json.net中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!