多对象JSON,“无法反序列化当前的JSON对象”

时间:2015-04-18 16:22:17

标签: c# json c#-4.0 json.net

好的,首先,答案可能非常简单......但是经过45分钟的尝试和google后,我决定要求stackoverflow。对不起......谢谢你的时间!


所以我有一些问题让这个Json正确解析。我使用http://json2csharp.com/创建了类,但它没有告诉我解析它的代码。

我目前的班级:

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
}

和代码:

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

再次为蹩脚的问题感到抱歉,感谢你的时间!

1 个答案:

答案 0 :(得分:3)

您应该反序列化为SearchQuery,而不是List<SearchQuery>

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

然后使用search属性访问搜索结果列表:

List<Search> searchResults = result.search;