Reputation: 18521
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.
Upvotes: 1
Views: 5986
Reputation: 21
One way to get it is using linq.
resultList.Where((obj, i) => (obj) %2!=0);
Upvotes: 1
Reputation: 743
Use following from Linque.
resultList.Where((child, index) => (index) %2!=0);
I hope it will help u.
Upvotes: 4
Reputation: 7689
One way to de-serialize your JSON is by using Newtonsoft (instead of loops);
List<string> myList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(resultList);
And then my simple linq, you may filter your data.
Upvotes: 1