How to get specific value from JSON array using C#

Advertisements

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

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];       

Leave a ReplyCancel reply