3

Im with problem deserialize Json with newtonsoft json.

I have a class

[Serializable]
public class ValueAdd
{
    [JsonProperty(PropertyName = "@id")]
    public string Description { get; set; }

    [JsonProperty(PropertyName = "@description")]
    public string Id { get; set; }
}

[Serializable]
public class ValueAdds
{ 
    public List<ValueAdd> ValueAdd { get; set; }

    [JsonProperty(PropertyName = "@size")]
    public string Size { get; set; }
}

And the API returning the stupid two things of format: One i can serialize correct:

"ValueAdds":
{
   "@size": "3",
   "ValueAdd":
   [
    {
        "@id": "2103",
        "description": "some property"
    },
    {
        "@id": "2192",
        "description": "some property"
    },
    {
        "@id": "2196",
        "description": "some property"
    }
   ]
}

But when they return one property it didnt return a list.. only return on this way:

"ValueAdds":
{
   "@size": "1",
   "ValueAdd":
    {
        "@id": "2103",
        "description": "some property"
    }
}

Causing me a parser error with

JsonConvert.DeserializeObject<ValueAdds>(_response);

A first chance exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[myproperty]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

My question is, is there anyway to fix it?? I cant change the api response, need change from my side trying to parse if there is a list or not..

PaonX
  • 31
  • 2
  • http://json2csharp.com/ might help you out for creating a proper model – online Thomas Aug 23 '16 at 13:40
  • 1
    For a proper solution you must consider to handle both situations bu using a custom `JsonConverter` explained in [another post](http://stackoverflow.com/a/18997172/969278) – Halis S. Aug 23 '16 at 13:46
  • Use `SingleOrArrayConverter` from [How to handle both a single item and an array for the same property using JSON.net](https://stackoverflow.com/questions/18994685/how-to-handle-both-a-single-item-and-an-array-for-the-same-property-using-json-n). – dbc Aug 23 '16 at 17:05

2 Answers2

0

Actually, property should be either array or object. Your response should be consistent.

Anyway, to solve the problem, you should bind it to dynamic or object.

[Serializable]
public class ValueAdds
{ 
 public dynamic ValueAdd { get; set; }

 [JsonProperty(PropertyName = "@size")]
 public string Size { get; set; }
}

Then you should check the ValueAdd type.

omer faruk
  • 350
  • 4
  • 13
-1

Because in your ValueAds ValueAdd is list but in your second json its a single entity so you can not assign it, you need to create another model

[Serializable]
public class ValueAdds2
{ 
    public ValueAdd ValueAdd { get; set; }

    [JsonProperty(PropertyName = "@size")]
    public string Size { get; set; }
}

then

JsonConvert.DeserializeObject<ValueAdds2>(_response);
Mostafiz
  • 7,243
  • 3
  • 28
  • 42