skip to Main Content

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"
    }
]

2

Answers


  1. 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];       
    
    Login or Signup to reply.
  2. JArray jArray = JsonConvert.DeserializeObject<JArray>(jsonstring);
    
    string[] valueArray = jArray.Select(p => p["Bur"]["RiskSummary"["RiskId"].ToString()).ToArray();
    
    string value = string.Join(":", valueArray);`
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search