多对象 JSON,"无法反序列化当前 JSON 对象"



好吧,首先,答案可能很简单...但是经过 45 分钟的尝试和谷歌搜索,我就是想不通!

<小时 />

所以我在正确解析这个 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; }
}

杰森:

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

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

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

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

List<Search> searchResults = result.search;

相关内容

  • 没有找到相关文章

最新更新