搜索DeserializeObject JSON

时间:2018-02-13 10:19:31

标签: c# json json.net

我的DeserializeObject如下所示。

如何根据Value使用 Linq (或任何其他方式)查找Key

var dictionaryList = JsonConvert.DeserializeObject<dynamic>(response);

{{
  "Resources": [
    {
      "Key": "HeadingCustomerSegments",
      "Value": "Customer segments"
    },
    {
      "Key": "Clear all",
      "Value": "Clear all"
    },
    {
      "Key": "Third selection of stores the report will be based on",
      "Value": "Third selection of stores the report will be based on"
    },
    {
      "Key": "Select the stores to be included in the Dashboard/Opportunity",
      "Value": "Select the stores to be included in the Dashboard/Opportunity"
    },
}}

2 个答案:

答案 0 :(得分:0)

如果你想在没有具体课程的情况下使用Linq,你可以这样做:

var dictionaryList = (JObject)JsonConvert.DeserializeObject(@"{
        ""Resources"": [
          {
      ""Key"": ""HeadingCustomerSegments"",
            ""Value"": ""Customer segments""
    },
    {
    ""Key"": ""Clear all"",
      ""Value"": ""Clear all""

    },
    {
    ""Key"": ""Third selection of stores the report will be based on"",
      ""Value"": ""Third selection of stores the report will be based on""

    },
    {
    ""Key"": ""Select the stores to be included in the Dashboard/Opportunity"",
      ""Value"": ""Select the stores to be included in the Dashboard/Opportunity""

    }]
}");

var element = dictionaryList["Resources"]?.FirstOrDefault(x => x["Key"].Value<string>() == "HeadingCustomerSegments");
var value = element != null ? element["Value"]?.Value<string>() : null;
Console.WriteLine(value);

一个更具体的方法,具体的类,就是这样:

void Main()
{
    var dictionaryList = JsonConvert.DeserializeObject<Response>(@"{
            ""Resources"": [
              {
          ""Key"": ""HeadingCustomerSegments"",
                ""Value"": ""Customer segments""
        },
        {
        ""Key"": ""Clear all"",
          ""Value"": ""Clear all""

        },
        {
        ""Key"": ""Third selection of stores the report will be based on"",
          ""Value"": ""Third selection of stores the report will be based on""

        },
        {
        ""Key"": ""Select the stores to be included in the Dashboard/Opportunity"",
          ""Value"": ""Select the stores to be included in the Dashboard/Opportunity""

        }]
    }");

    var value = dictionaryList.Resources.Where(r => r.Key == "HeadingCustomerSegments").Select(r => r.Value).FirstOrDefault();
    Console.WriteLine(value);
}

public class Response
{
    public List<Resource> Resources { get; set; }
}

public class Resource
{
    public string Key { get; set; }
    public string Value { get; set; }
}

答案 1 :(得分:0)

对于您提供的回复,您可以使用:

var listsOfResources = JObject.Parse(response).SelectToken("Resources").ToList();

但如果您愿意,也可以尝试生成字典:

var dictionaryResources = JObject.Parse(response).SelectToken("Resources").ToObject<Dictionary<string, string>>();

从listsOfResources获取单个项目,您可以使用例如ExpandoObject:

var item = JsonConvert.DeserializeObject<ExpandoObject>(listsOfResources[0].ToString());