I have the following JSON format delivered by a third party:
{
"data": {
"objects": {
"0ea73fa9333a7cbeb2d8c69a14b9970f": {
"Id": "0ea73fa9333a7cbeb2d8c69a14b9970f",
"Name": "test"
},
"38b1390ff6bc8a9837105d181000bcc8": {
"Id": "38b1390ff6bc8a9837105d181000bcc8",
"Name": "test"
}
}
}}
which i'm deserializing using this model:
public class ObjectTypes
{
public ObjectTypeList data { get; set; }
public class ObjectTypeList
{
public Dictionary<string, Object> objects { get; set; }
}
public class Object
{
public string Id { get; set; }
public string Name { get; set; }
}
}
This works fine, however sometimes the JSON is empty like this:
{
"data": {
"objects": []
}
}
Which results in this exception:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,Models.ObjectTypes+Object]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'data.objects', line 1, position 20.
Any suggestions on how to resolve this?
objectsis not an array[]its object{}so hereDictionary<string, Object>doesn't work here - er-shoObject. - spender