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 get specific value from JSON array using C#

I need help trying to extract "RiskID" value from the attached JSON using C# and Newtonsoft.

The response is JSON array and that is why I’m having some difficulty.

I tried to do some testing using below code but it is not working

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

JArray jArray = JsonConvert.DeserializeObject<JArray>(jsonstring);

                for (int i = 0; i < jArray.Count; i++)
                {
                    JArray jArray1 = JsonConvert.DeserializeObject<JArray>(jArray[i].ToString());
                    string key = ((JValue)jArray1[0]).Value == null ? "" : ((JValue)jArray1[0]).Value.ToString();
                    string value = ((JValue)jArray1[1]).Value == null ? "" : ((JValue)jArray1[1]).Value.ToString();
                    Response.Write(key + " : " + value +"<br>");
                }

Thanks for your help.

[
    {
        "Bur": {
            "RiskSummary": {
                "Address": {
                    "StreetAddress1": "423 W OLYMPIC BLVD",
                    "City": "MONTEBELLO",
                    "PostalCity": "MONTEBELLO",
                    "State": "CA",
                    "Zip": "90640",
                    "Zip4": "5225",
                    "County": "LOS ANGELES"
                },
                "FPA": "MONTEBELLO",
                "RiskId": "64CX99016399",
                "OnsiteSurveyDate": "05/01/1999",
                "ScheduleAppliedDate": "04/21/1999",
                "YearBuilt": "1960"
            },
            "Comments": {
                "GeneralComments": [],
                "WindComments": "",
                "WindLossHistory": ""
            }
        },
        "Pdr": null,
        "Bov": null,
        "ReportCode": "BUR"
    },
    {
        "Bur": {"RiskSummary": {
                "Address": {
                    "StreetAddress1": "423 W OLYMPIC BLVD",
                    "City": "MONTEBELLO",
                    "PostalCity": "MONTEBELLO",
                    "State": "CA",
                    "Zip": "90640",
                    "Zip4": "5225",
                    "County": "LOS ANGELES"
                },
                "FPA": "MONTEBELLO",
                "RiskId": "64CX99012345",
                "OnsiteSurveyDate": "05/01/1999",
                "ScheduleAppliedDate": "04/21/1999",
                "YearBuilt": "1960"
            },
            "Comments": {
                "GeneralComments": [],
                "WindComments": "",
                "WindLossHistory": ""
            }
        },
        "Pdr": null,
        "Bov": null,
        "ReportCode": "BUR"
    }
]

>Solution :

you have to parse your json string to json array

    string[] riskIds = JArray.Parse(jsonstring)
                             .Select(a => a.SelectToken("Bur.RiskSummary.RiskId").Value<string>())
                             .ToArray();
                             
    string  riskId0 = riskIds[0];            
    string  riskId1 = riskIds[1];       
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