I’m using the json.net (Newtonsoft) library to parse an API json response. I only need the id values and the rank values. The json looks like this:
{"items":
[
{"fields":
{"entry":
[
{"key":"Customer","value":"ABC Electronics"},
{"key":"Status","value":"Untouched"},
{"key":"Amount","value":"$1000"},
{"key":"rank","value":"3.0"}
]
},
"id":"1001",
"owner":"dave@sales123.com"
},
{"fields":
{"entry":
[
{"key":"Customer","value":"General Products"},
{"key":"Status","value":"Developing"},
{"key":"Amount","value":"$6000"},
{"key":"rank","value":"6.0"}
]
},
"id":"1002",
"owner":"john@sales123.com"
}
]
}
I have a method inside my class that makes the request, loops through the json and grabs the id and rank, then adds those values as a new RankScore object to a List class member. My question is if there is a better way to find the “value” where the key == “rank” in the entry array than just looping through it and checking each key. Any LINQ way that I’ve been unable to find?
public void MakeRequest(apiURL) {
var requestUrl = new Uri(apiUrl, "/ranks/0");
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
request.Credentials = new NetworkCredential(this.Username, this.Password);
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
var token = JObject.Parse(reader.ReadToEnd());
var items = token["items"];
foreach (var item in items)
{
var id = item.Value<string>("id");
var entries = item["fields"]["entry"];
string rank = null;
// **** Is there a better way to do this? ****
foreach (var entry in entries)
{
if (entry.Value<string>("key") == "rank")
{
rank = entry.Value<string>("value");
break;
}
}
var ranking = new RankScore(int.Parse(id), rank);
this.Ranks.Add(ranking);
}
}
}
}
Simple answer, no. You could let something else do some of the looping for you but it’s not like you’re going to come up with a solution that performs better by using LINQ (it’s also just looping over the collection).
If your goal is simply to clean up your code you could serialize into an object, then you’ll have a simpler loop in your code (of course json.NET will be doing plenty of looping);
Although I personally wouldn’t recommend that. I like the simple method. Another (stupid) idea would be to use RegEx. You would have fewer lines but much worse performance. Basically, you’re not going to remove iteration, you can use an abstraction to do some of it but there’s really no point. I would just stick with what you have unless you’re going to do a lot more with the json. If you intend to do more with the json elsewhere then I would serialize into an object.