Showing posts with label REST JSON. Show all posts
Showing posts with label REST JSON. Show all posts

Wednesday, October 31, 2012

JsonConvert.DeserializeObject - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.String]'


Run into this issue while consuming a 3rd party JSON API using JSON.NET.

Complete exception message:

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

The json string I was trying to deserialize had a field looking like this:

"extras": {},

In my C# class that field was declared as a List<string>. 
By declaring the field as an IDictionary<string, string> instead the problem was solved. 

Lesson learned:
  • If the json value is '[]' => declare the field as List<type>
  • If the json value is '{}' => declare the field IDictionary<type, type>