Im trying to get a list of all the "Occupants" and "RiskId" from the JSON below using C# and Newtonsoft.
Not sure what I’m doing wrong here. Thanks for your help.
{
"Header": {
"MatchType": "Exact",
"TotalResultCount": 2,
"PreviousPageUrl": null,
"NextPageUrl": null,
"AvailableProductsForRequestedAddress": "https://prometrixapiuat.iso.com/uw/address/productList?City=MONTEBELLO&State=CA&StreetName=423+W+OLYMPIC+BLVD&Zip=90640"
},
"Risks": [
{
"RiskId": "64CX99016399",
"BuildingDescription": "MONTEBELLO PLASTICS INC (1S)",
"SurveyDates": {
"ScheduleAppliedDate": "04/21/1999",
"LatestPhoneSurveyDate": "",
"OnsiteSurveyDate": "05/01/1999"
},
"Location": {
"Latitude": null,
"Longitude": null,
"Address": {
"StreetAddress1": "423 W OLYMPIC BLVD",
"City": "MONTEBELLO",
"PostalCity": "MONTEBELLO",
"State": "CA",
"Zip": "90640",
"Zip4": "5225",
"County": "LOS ANGELES"
},
"AlternateAddresses": []
},
"Occupants": [
{
"Name": "MFG STORAGE - PACKAGING MATERIALS",
"OccupantId": "15"
}
],
"ProductListUrl": "https://prometrixapiuat.iso.com/uw/Risk/64CX99016399/ProductList",
"YearBuilt": 1960,
"FireProtectionArea": "MONTEBELLO"
},
{
"RiskId": "64CX99016400",
"BuildingDescription": "MONTEBELLO PLASTICS INC (1S)",
"SurveyDates": {
"ScheduleAppliedDate": "04/21/1999",
"LatestPhoneSurveyDate": "",
"OnsiteSurveyDate": "05/01/1999"
},
"Location": {
"Latitude": null,
"Longitude": null,
"Address": {
"StreetAddress1": "423 W OLYMPIC BLVD",
"City": "MONTEBELLO",
"PostalCity": "MONTEBELLO",
"State": "CA",
"Zip": "90640",
"Zip4": "5225",
"County": "LOS ANGELES"
},
"AlternateAddresses": []
},
"Occupants": [
{
"Name": "MFG STORAGE - PACKAGING MATERIALS",
"OccupantId": "15"
}
],
"ProductListUrl": "https://prometrixapiuat.iso.com/uw/Risk/64CX99016400/ProductList",
"YearBuilt": 1960,
"FireProtectionArea": "MONTEBELLO"
}
]
}
I would just like to loop through the list of "RiskId" and "Occupants" and display the value.
If I do ….
var jsonObj = (JObject)JsonConvert.DeserializeObject(jsonstring);
var jsonResult = jsonObj.SelectToken("Risks.RiskId").Select(m => m.Value<string>());
I’m getting below error
System.ArgumentNullException: ‘Value cannot be null.
Parameter name: source’
>Solution :
I showed you this kind of code yesterday
JArray risks = (JArray) JObject.Parse(jsonstring)["Risks"];
var riskId = risks.Select(r => (string) r["RiskId"] ).FirstOrDefault(); // "64CX99016400"
JArray occupants = risks.Select(r => (JArray) r["Occupants"] ).FirstOrDefault());
UPDATE
after @dbc update, if you can have several risks items, just remove firstordefault()
string[] riskId = risks.Select(r => (string) r["RiskId"] )
Response.Write(string.Join(",",riskId)); //"64CX99016400"
JArray occupants = risks.Select(r => (JArray) r["Occupants"] );