如何将 JObject 反序列化为 .NET 对象 [英] How to deserialize a JObject to .NET object
本文介绍了如何将 JObject 反序列化为 .NET 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很高兴使用 Newtonsoft JSON 库.例如,我将从 .NET 对象创建一个 JObject
,在这种情况下是一个 Exception 实例(可能是也可能不是子类)
I happily use the Newtonsoft JSON library.
For example, I would create a JObject
from a .NET object, in this case an instance of Exception (might or might not be a subclass)
if (result is Exception)
var jobjectInstance = JObject.FromObject(result);
现在我知道库可以将 JSON 文本(即字符串)反序列化为对象
now I know the library can deserialize JSON text (i.e. a string) to an object
// only works for text (string)
Exception exception = JsonConvert.DeserializeObject<Exception>(jsontext);
但我正在寻找的是:
// now i do already have an JObject instance
Exception exception = jobjectInstance.????
很明显,我可以从 JObject
返回到 JSON 文本,然后使用反序列化功能,但这对我来说似乎是倒退.
Well it is clear that I can go from on JObject
back to JSON text and then use the deserialize functionality, but that seems backwards to me.
推荐答案
根据此 post,现在好多了:
According to this post, it's much better now:
// pick out one album
JObject jalbum = albums[0] as JObject;
// Copy to a static Album instance
Album album = jalbum.ToObject<Album>();
文档:将 JSON 转换为类型
这篇关于如何将 JObject 反序列化为 .NET 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文