Search code examples
c#jsonlinqjson.net

Get a list from a dynamic json result


using

dynamic result = JsonConvert.DeserializeObject(jsonResult);

I get the following Json

{
 facet_counts: {
  facet_queries: { },
   facet_fields: {
     Suggest: [
      "AAA",
      0,
      "BBB",
      0,
      "CCC",
      0,
      "DDD",
      0,
      "EEE",
      0]
},

I want to iterate "Suggest" get a list of the values in odds location (1,3,5..).
i.e. "AAA","BBB","CCC","DDD"

I can get them all using basic

dynamic resultList = result.facet_counts.facet_fields.Suggest.Children();
foreach (dynamic child in resultList)
{
   strings.Add(child.ToString());
}

But since all the dynamic fields are JVAlue and I have to use an index and add a condition, I wonder if there is a more elegant way (maybe linq?).
Thanks.


Solution

  • Use following from Linque.

    resultList.Where((child, index) => (index) %2!=0); 
    

    I hope it will help u.