JSON.NET:反序列化JSON对象的一部分字典 [英] JSON.NET: Deserializing part of a JSON object to a dictionary
本文介绍了JSON.NET:反序列化JSON对象的一部分字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有JSON是这样的:
I have JSON like this:
{
"Property":"Blah blah",
"Dictionary": {
"Key1" : "Value1",
"Key2" : "Value2",
"Key3" : "Value3"
}
}
我要提取的字典对象作为字典(所以它会像键1 =>值1,等等)。如果我刚结束了说文解字的对象直接,我可以使用:
I want to extract the "Dictionary" object as a Dictionary (so it'd be like Key1 => Value1, etc.). If I just had the "Dictionary" object directly, I could use:
JsonConvert.DeserializeObject<Dictionary<string, string>>
什么是得到公正的字典属性作为字典的最佳方式是什么?
What's the best way to get just the Dictionary property as a Dictionary?
在此先感谢! 蒂姆·
推荐答案
我花了一些时间来弄清楚,但我只是没有感觉很好用字符串解析或正则表达式来获得的,我想内部JSON。
Took me a little while to figure out, but I just didn't feel great about using string parsing or regexes to get at the inner JSON that I want.
够简单;我做了一件沿着这些路线来获得的内部数据:
Simple enough; I did something along these lines to get at the inner data:
var jObj = JObject.Parse(jsonText);
var innerJObj = JObject.FromObject(jObj["Dictionary"]);
作品不够好。
Works well enough.
这篇关于JSON.NET:反序列化JSON对象的一部分字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文