I have a class that calls a webservice which returns a JSON string which I need to deserialize into a C# object. I was successfully able to do this; however, I came across a scenario that I am not sure exactly the best way to handle. More specifically, the JSON will either return a List<List<object>> or just a List<object>. I am having a problem when I deserialize if my object is List<object> and the JSON is List<List<object>>. In that case, an exception is thrown.
This is class that I am trying to deserialize:
public class WorkOrderJson
{
public string type { get; set; }
public Properties properties { get; set; }
public Geometry geometry { get; set; }
}
public class Properties
{
public string FeatureType { get; set; }
public string WorkOrderID { get; set; }
public string EqEquipNo { get; set; }
}
For the Geometry class the coordinates returned are the issue from above. If the JSON returned is a List<List<object>> it serializes fine.
public class Geometry
{
public string type { get; set; }
public List<List<double>> coordinates { get; set; }
}
This is how I am performing deserialization:
WorkOrderJson workOrderJson = new JavaScriptSerializer().Deserialize<List<WorkOrderJson>>(responseString);
where responseString is the JSON string returned from web service. Hope this makes sense. If anybody has come across a similar issue, any help would be much appreciated.
Here is an example for List<List<object>> where coordinates is the list:
[
{
"type": "Feature",
"properties": {
"FeatureType": "WORKORDER",
"WorkOrderID": "AMO172-2015-107",
"EqEquipNo": "AC-LIN-001"
},
"geometry": {
"type": "LineString",
"coordinates": [
[
-111.00041804208979,
33.0002148138019
],
[
-111.00027869450028,
33.000143209356054
]
]
},
"symbology": {
"color": "#990000",
"lineWidth": "8"
}
}
]
Here is an example for List<object>:
[
{
"type": "Feature",
"properties": {
"FeatureType": "WORKORDER",
"WorkOrderID": "AMO172-2015-115",
"EqEquipNo": "AC-LIN-001"
},
"geometry": {
"type": "Point",
"coordinates": [
-111.00041804208979,
33.0002148138019
]
}
}
]