0

My DeserializeObject is listed below.

How can I use Linq (or any other way) to find Value based on 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"
    },
}}
Dean Seo
  • 5,486
  • 3
  • 30
  • 49
Emil
  • 281
  • 1
  • 2
  • 11
  • 2
    Rather than using `dynamic`, deserialize to the explicit type `JToken` and use [`SelectTokens()`](https://www.newtonsoft.com/json/help/html/QueryJsonSelectTokenJsonPath.htm). See [What is the JSON.NET equivalent of XML's XPath, SelectNodes, SelectSingleNode?](https://stackoverflow.com/q/1698175/3744182) and [Searching for a specific JToken by name in a JObject hierarchy](https://stackoverflow.com/q/19645501/3744182) – dbc Feb 13 '18 at 10:21
  • Or use a proper concrete type if the structure isn't going to change. Plenty of sites out there to convert JSON into C# classes and this one looks like a very simple structure. – DavidG Feb 13 '18 at 10:29

2 Answers2

0

In case you want to do it with Linq without a concrete class you could do it this way:

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);

A more proper way, with a concrete class, would be like this:

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; }
}
Jeroen Vervaeke
  • 1,040
  • 9
  • 20
0

For response that you've provided you can use:

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

but you can try also generate dictionary if you want:

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

for getting single item from listsOfResources you can use e.g. ExpandoObject:

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