{"data": {"day2": {"mId": "9ilrMdX15S", "votes": "2,893"},"day3": {"mId": "9ilert415S","votes": "2,343"}}}
How can i retrieve the data from a json data as such (i.e the data in "day2", or "day3") i followed the answers here > Appending json data to listview c# by Brain Rogers but the answer only work for json object not for nested json.
-
Its not really nested I think. You could see this as a class with a single array/list containing 'day' objects that have their own properties.EpicKip– EpicKip2021-06-22 09:24:48 +00:00Commented Jun 22, 2021 at 9:24
-
I think the issue is solved when the days are not written as day1, day2, day3 but an array where a day could have an explicit index.Mike de Klerk– Mike de Klerk2021-06-22 09:25:39 +00:00Commented Jun 22, 2021 at 9:25
-
@MikedeKlerk I fully overlooked that detail you're correctEpicKip– EpicKip2021-06-22 09:26:07 +00:00Commented Jun 22, 2021 at 9:26
-
Will it carry on to infinite number of dayX properties? Can you influence the structure of the Json or are you stuck with it?Caius Jard– Caius Jard2021-06-22 09:27:49 +00:00Commented Jun 22, 2021 at 9:27
-
i am inputting the day2, day3, objects manually so i basically want to retrieve the data of "data>day2> {all the data inside day2}" only or the data inside day3Serenity Emmanuel– Serenity Emmanuel2021-06-22 09:35:24 +00:00Commented Jun 22, 2021 at 9:35
|
Show 2 more comments
4 Answers
If you're on .NET 6+, you don't need an external library. Just add this namespace:
using System.Text.Json.Nodes;
Then you can do this:
string jsonInput = "{\"data\": {\"day2\": {\"mId\": \"9ilrMdX15S\", \"votes\": \"2,893\"},\"day3\": {\"mId\": \"9ilert415S\",\"votes\": \"2,343\"}}}";
JsonObject? jsonObject = JsonNode.Parse(jsonInput)?.AsObject();
string? votes = (string?)jsonObject?["data"]?["day2"]?["votes"]; // outputs 2,893
Comments
adding to @Tawab Wakil's answer: If there are inner array's in your JSON like the following:
{
"rootNode": [
{
"id": 1,
"name": "testUser",
"attachedBag":
{
"content":
{
"items": [
{
"itemid": 322,
"itemName": "orange",
"price": 0.99,
"qty": 4
},
{
"itemid": 323,
"itemName": "apple",
"price": 0.49,
"qty": 5
}
]
},
"type": "subscriber"
}
}
]
}
and you wanted to get a list of items like "oranges", "apples", then you can do the following assuming your json is stored in a variable called "jsonResponse":
//"jsonResponse" contains the raw json as a string variable
var responseJsonObj = JsonNode.Parse(jsonResponse)?.AsObject();
var itemsList = responseJsonObj?["rootNode"]?.AsArray()[0]?["attachedBag"]?["content"]?["items"].AsArray();
var outputStrList = "";
foreach (var itemx in itemsList )
{
outputStrList += " - " + itemx.AsObject()["itemName"] + "\n";
}
//Output
// - orange
// - apple
Comments
Here's an example of how to navigate it:
foreach (JProperty day in JObject.Parse(json)["data"])
{
string name = day.Name;
string id = day.Value.Value<string>("mId");
string votes = day.Value.Value<string>("votes");
}
1 Comment
Serenity Emmanuel
Thanks but this only retrieve the first value that is day2. How do i retrieve the data for day3 only
Thanks for all the response, i was able to solve the task using the SimpleJSON library by Bunny83 GitHub - Bunny83/SimpleJSON: A simple JSON parser in C#
JSONNode data = JSON.Parse(//jsonData or Url//);
string mId = data["data"]["day2"]["mId"].Value;
string votes = data["data"]["day2"]["votes"].Value;
1 Comment
Hui Liu
Hi, @ Serenity Emmanuel. Does this solution solve your problem? If it is solved, you can click ‘✔’ to accept it as an answer. It is helpful for community members to solve the similar problems.