user2598794
user2598794

Reputation: 727

Cannot deserialize object with Json.Net

I have a very simple POCO class:

    [JsonArray]
    public class WebGame
    {    
        public  int Key { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }
    }

Trying to deserialize to it a json string:

  string json = @"[{'Key':'1','Name':'WebGame','Description':'bla-bla-bla'}]";
  var myDeserializedObjList = (List<WebGame>)(JsonConvert.DeserializeObject(json, typeof(List<WebGame>)));      
  var game = myDeserializedObjList[0];

I get an exception:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'WebGame' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

What's wrong here?

Upvotes: 1

Views: 331

Answers (1)

Manvik
Manvik

Reputation: 977

Remove [JsonArray] attribute and it would work fine

Upvotes: 1

Related Questions