I am trying to use RestSharp to Deserialize a JSON string. However I am getting caught on the slightly non standard structure around the "555111222" part.
The number 555111222 is a device serial number so it can change or there can be multiple serial numbers in the JSON response.
{
"requestid": "42",
"data": {
"555111222": {
"gps_data": [
{
"longitude": -73.952284,
"latitude": 40.755988,
"time": "2016-06-30 09:41:21"
},
{
"longitude": -73.952284,
"latitude": 40.755988,
"time": "2016-06-30 09:41:22"
},
{
"longitude": -73.952284,
"latitude": 40.755988,
"time": "2016-06-30 09:41:23"
}
]
}
}
}
Json2csharp.com gives me the following
public class GpsData
{
public double longitude { get; set; }
public double latitude { get; set; }
public string time { get; set; }
}
public class __invalid_type__555111222
{
public List<GpsData> gps_data { get; set; }
}
public class Data
{
public __invalid_type__555111222 __invalid_name__555111222 { get; set; }
}
public class RootObject
{
public string requestid { get; set; }
public Data data { get; set; }
}
Im not sure how to handle the invalid_type errors in my class.
Thank you
It looks to me like your Data
property should be Dictionary<string, GpsDataWrapper>
where GpsDataWrapper
is just a class with a GpsData
property:
public class GpsData
{
public double longitude { get; set; }
public double latitude { get; set; }
public string time { get; set; }
}
public class GpsDataWrapper
{
public List<GpsData> gps_data { get; set; }
}
public class RootObject
{
public string requestid { get; set; }
public Dictionary<string, GpsDataWrapper> data { get; set; }
}
I'd personally then convert all of these to use .NET naming conventions and apply attributes to specify the names used in JSON.
See more on this question at Stackoverflow