79091798

Date: 2024-10-15 21:29:54
Score: 0.5
Natty:
Report link

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
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Tawab
  • Low reputation (1):
Posted by: Shahriar chandon