0

{"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.

7
  • 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. Commented 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. Commented Jun 22, 2021 at 9:25
  • @MikedeKlerk I fully overlooked that detail you're correct Commented 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? Commented 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 day3 Commented Jun 22, 2021 at 9:35

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

0

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

Thanks but this only retrieve the first value that is day2. How do i retrieve the data for day3 only
0

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.