简体   繁体   English

Json.Net键/值对字典对象

[英]Json.Net Key/Value Pair to dictonary object

I am trying to extract the below key-value pair in JSON to dictonary. 我正在尝试将JSON中的以下键值对提取到字典。

var str = @" 
{
   ""tables"": {
    ""category"": ""Category"",
    ""subcategory"": ""SubCategory"",
    ""tfs"": ""tfs"",
    ""tickets"": ""snowtickets"",
    ""ticketcategoryauto"": ""TicketCategoryAuto"",
    ""ticketcategoryuserselected"": ""TicketCategoryManual""
  }
}
";


  var jo = JObject.Parse(str);
  var x = from c in jo["tables"] select c;

I want the "tables" node of this Json into a dictionary object. 我希望将此Json的“表”节点转换成字典对象。 so if I say x["category"] should get back "Category" similarly for other keys. 因此,如果我说x [“ category”]应该为其他键同样返回“ Category”。 How can I do that. 我怎样才能做到这一点。

tables is a dictionary type object so you need to return it as a type of Dictionary . tables是一个字典类型的对象,因此您需要将其作为Dictionary一种类型返回。

var jo = JObject.Parse(str);
Dictionary<string, string> values = jo["tables"].ToObject<Dictionary<string,string>>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM