I have a string called rawJsonPostCodeString that looks like the following JSON.
{
"status": 200,
"result": {
"postcode": "SW1A 1AA",
"quality": 1,
"eastings": 529090,
"northings": 179645,
"country": "England",
"nhs_ha": "London",
"longitude": -0.141588,
"latitude": 51.501009,
"european_electoral_region": "London",
"primary_care_trust": "Westminster",
"region": "London",
"lsoa": "Westminster 018C",
"msoa": "Westminster 018",
"incode": "1AA",
"outcode": "SW1A",
"parliamentary_constituency": "Cities of London and Westminster",
"admin_district": "Westminster",
"parish": "Westminster, unparished area",
"admin_county": null,
"admin_ward": "St James's",
"ced": null,
"ccg": "NHS Central London (Westminster)",
"nuts": "Westminster",
"codes": {
"admin_district": "E09000033",
"admin_county": "E99999999",
"admin_ward": "E05000644",
"parish": "E43000236",
"parliamentary_constituency": "E14000639",
"ccg": "E38000031",
"ced": "E99999999",
"nuts": "UKI32"
}
}
}
I also have a class called PostCodeLocation
that looks like the below:
class PostCodeLocation
{
public double longitude { get; set; }
public double latitude { get; set; }
}
I have been using Json.Net to parse this data and then assign it to two items in the class.
Previously I have been using a code like the below to fetch data from a list with multiple results but when I try adapting this to read from my JSON for the post codes it throws an error on the JArray.Parse line for the format being invalid. I have not done any work with JSON before so I am unsure of the best way to process it.
public List<PostCodeLocation> ProcessJSONPostCodeLocation(string JSonPostCodes)
{
JArray PCDetails = JArray.Parse(JSonPostCodes);
List<JToken> results = PCDetails.Children().ToList();
List<PostCodeLocaton> PCResults = new List<PostCodeLocation>();
foreach (JToken result in results)
{
PCResults.Add(result.ToObject<PostCodeLocation>());
}
return PCResults;
}
If anyone has any tips it would be greatly appreciated.