I need some help with converting JSON file to C# object. I've been using Json.NET library. JSON file format are as below:
{"174.845620 -36.913447 WGS84":[{"uuid":"a7e72b5c1fb96f1452d3c64fe89c7e6a","name":"35 Carbine Road","suburb":"Mount Wellington","town":"Auckland","district":"Auckland City","region":"Auckland","island":"North Island","x":2674839,"y":6474828,"longitude":174.845707,"latitude":-36.913385,"locality":"Mount Wellington, Auckland, Auckland City"}],"174.698503 -36.788258 WGS84":[{"uuid":"96fb8ae43b6791f5f2b7006d8818b9ad","name":"1\/248 Beach Haven Road","suburb":"Birkdale","town":"North Shore","district":"North Shore City","region":"Auckland","island":"North Island","x":2661988,"y":6488992,"longitude":174.698375,"latitude":-36.78816,"locality":"Birkdale, North Shore, North Shore City"}]}
I've created the following classes like this to map the JSON:
public class WGS84Coordinate
{
public string uuid{get; set;}
public string name{ get; set;}
public string suburb { get; set;}
public string town { get; set;}
public string district { get; set;}
public string region { get; set;}
public string island { get; set;}
public int x { get; set;}
public int y { get; set;}
public double longitude { get; set;}
public double latitude { get; set;}
public string locality { get; set;}
}
public class WGS84Coordinates
{
public WGS84Coordinate wgs84Coodinate{ get; set;}
}
and I have the following code to deserialize the JSON:
List<WGS84Coordinates> r = JsonConvert.DeserializeObject<List<WGS84Coordinates>>(json);
if (r.Count > 0)
{
json = r[0].wgs84Coodinate.uuid + r[0].wgs84Coodinate.suburb;
}
The code doesn't seem to be working. Do I miss anything? Please do help. Many thanks. Chris
Edited btw, I've tried the following by using Dictionary and still not good. ERROR: "Cannot deserialize JSON array into type 'JSONConvertTester.WGS84Coordinate'."
Dictionary<string, WGS84Coordinate> r = JsonConvert.DeserializeObject<Dictionary<string, WGS84Coordinate>>(json); // deserialize
foreach (KeyValuePair<string, WGS84Coordinate> o in r)
{
json = o.Value.uuid;
}
Please anyone kindly advise, Many thanks.