Isa
Isa

Reputation: 248

Multi-Object JSON, "Cannot deserialize the current JSON object"

Okay first of all, the answer is probably very simple... But after 45 minutes of trying and googling I just can't figure it out!


So I have some problems getting this Json to parse correctly. I created the classes with http://json2csharp.com/ only it doesn't tell me the code to parse it.

My current classes:

public class Representations
{
    public string thumb { get; set; }
    public string large { get; set; }
    public string full { get; set; }
}

public class Search
{
    public string id { get; set; }
    public string file_name { get; set; }
    public Representations representations { get; set; }
}

public class SearchQuery
{
    public List<Search> search { get; set; }
    public int total { get; set; }
}

JSON:

    {
  "search": [
    {
      "id": "0300",
      "file_name": "0300.JPG",
      "representations": {
        "thumb": "thumb.jpg",
        "large": "large.jpg",
        "full": "0300.jpg"
      },
    },
    {
      "id": "0000",
      "file_name": "0000.JPG",
      "representations": {
        "thumb": "thumb.jpg",
        "large": "large.jpg",
        "full": "0000.jpg"
      },
    },
    {
      "id": "0d00",
      "file_name": "0d00.JPG",
      "representations": {
        "thumb": "thumb.jpg",
        "large": "large.jpg",
        "full": "0d00.jpg"
      },
    }
  ],
  "total": 3
}

and code:

searchresults = JsonConvert.DeserializeObject<List<SearchQuery>>(JSONCode);

Upvotes: 0

Views: 91

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039238

You should deserialize to a SearchQuery, not List<SearchQuery>:

SearchQuery result = JsonConvert.DeserializeObject<SearchQuery>(JSONCode);

and then use the search property to access the list of search results:

List<Search> searchResults = result.search;

Upvotes: 3

Related Questions