Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a list from a specific key inside the nested object of JSON response?

I’ve a JSON which I retrieved from var responseMessage1 = await request1.Content.ReadAsStringAsync(); and looks like this : –

{
    "operations": [
        {
            "creationTime": "2022-06-02T10:28:28.765+03:00",
            "deviceId": "43432103",
            "deviceName": "P25-SC-0228",
            "status": "PENDING",
            "com_cumulocity_model": {
                "op": "s",
                "param": "waterStartDateTime",
                "value": "1/2/2018, 7:30:00 AM"
            },
            "description": "Generate Plan"
        },
        {
            "creationTime": "2022-06-02T10:28:36.276+03:00",
            "deviceId": "43432103",
            "deviceName": "P25-SC-0228",
            "status": "PENDING",
            "com_cumulocity_model": {
                "Mode": 0,
                "StopStationPayload": "[{\"ControllerAddress\":11,\"StationAddress\":26}]"
            },
            "description": "Stop Station"
        }
    ],
    "statistics": {
        "currentPage": 1,
        "pageSize": 5
    }
}

How do I create a list of values inside the key description such that I can store it in a list like : –

var desc_list = ["Generate Plan","Stop Station"]
and I can iterate through it like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

foreach(var item in desc_list)
{
  //use item for xyz purpose
}

Which will output :

Generate Plan
Stop Station

>Solution :

Simple solution to do it convert JSON to C# objects and then iterate over Operation and read its Description property, as given below:

Root myDeserializedObject = JsonConvert.DeserializeObject<Root>(myJsonResponse);
foreach(var item in myDeserializedObject.operations)
{
   Console.WriteLine(item.description);
}

Generate these C# object from JSON string from: https://json2csharp.com/

public class ComCumulocityModel
{
    public string op { get; set; }
    public string param { get; set; }
    public string value { get; set; }
    public int? Mode { get; set; }
    public string StopStationPayload { get; set; }
}

public class Operation
{
    public DateTime creationTime { get; set; }
    public string deviceId { get; set; }
    public string deviceName { get; set; }
    public string status { get; set; }
    public ComCumulocityModel com_cumulocity_model { get; set; }
    public string description { get; set; }
}

public class Root
{
    public List<Operation> operations { get; set; }
    public Statistics statistics { get; set; }
}

public class Statistics
{
    public int currentPage { get; set; }
    public int pageSize { get; set; }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading