What is the simple way of querying JSON object instead of writing multiple foreachloops?
Sample JSON
{
"result_index": 0,
"results": [
{
"final": true,
"alternatives": [
{
"delta": 0.9,
"timestamps": [
[
"hi",
2.55,
2.81
]
]
}
]
}
]
}
Is there a way to replace multiple following foreach into single foreach
var rawData = JObject.Parse(responseString);
var results = rawData["results"];
foreach (var item in results)
{
foreach (var alternative in item["alternatives"])
{
foreach (var timestamp in alternative["timestamps"])
{
}
}
}
results, or which JSON library you're using, which makes it tricky to give a concrete answer. For example, Json.NET supports JSON Path which might help you (see newtonsoft.com/json/help/html/QueryJsonSelectTokenWithLinq.htm) but we don't know if you're using Json.NET or not...