0

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);

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Although List<Search> searchResults does not work for me. But Search searchResults does.
That depends on what type you have declared your searchResults variable with. From your code this doesn't get quite clear. In my example I have assigned the searchResults variable to a List<Search> but you are free to use whatever naming and types you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.