无法将JSON对象反序列化为类型'System.Collections.Generic.List' [英] Cannot deserialize JSON object into type 'System.Collections.Generic.List'

查看:190
本文介绍了无法将JSON对象反序列化为类型'System.Collections.Generic.List'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反序列化使用json.net从网络上提取的json字符串,但是我遇到了无法反序列化JSON对象的错误.这是json字符串

i'm trying to deserialize a json string pulled from the web using json.net, but i am getting a Cannot deserialize JSON object error. Here is the json string


{
    "metadata": {
        "page": 1,
        "perPage": 23,
        "count": 23
    },
    "results": [
        {
            "id": 9,
            "name": "Breaks",
            "slug": "breaks",
            "subgenres": []
        },
        {
            "id": 10,
            "name": "Chill Out",
            "slug": "chill-out",
            "subgenres": []
        },
        {
            "id": 12,
            "name": "Deep House",
            "slug": "deep-house",
            "subgenres": []
        },
        {
            "id": 16,
            "name": "DJ Tools",
            "slug": "dj-tools",
            "subgenres": []
        },
        {
            "id": 1,
            "name": "Drum & Bass",
            "slug": "drum-and-bass",
            "subgenres": []
        },
        {
            "id": 18,
            "name": "Dubstep",
            "slug": "dubstep",
            "subgenres": []
        },
        {
            "id": 17,
            "name": "Electro House",
            "slug": "electro-house",
            "subgenres": []
        },
        {
            "id": 3,
            "name": "Electronica",
            "slug": "electronica",
            "subgenres": []
        },
        {
            "id": 40,
            "name": "Funk / R&B",
            "slug": "funk-r-and-b",
            "subgenres": []
        },
        {
            "id": 49,
            "name": "Glitch Hop",
            "slug": "glitch-hop",
            "subgenres": []
        },
        {
            "id": 8,
            "name": "Hard Dance",
            "slug": "hard-dance",
            "subgenres": []
        },
        {
            "id": 2,
            "name": "Hardcore / Hard Techno",
            "slug": "hardcore-hard-techno",
            "subgenres": []
        },
        {
            "id": 38,
            "name": "Hip-Hop",
            "slug": "hip-hop",
            "subgenres": []
        },
        {
            "id": 5,
            "name": "House",
            "slug": "house",
            "subgenres": []
        },
        {
            "id": 37,
            "name": "Indie Dance / Nu Disco",
            "slug": "indie-dance-nu-disco",
            "subgenres": []
        },
        {
            "id": 14,
            "name": "Minimal",
            "slug": "minimal",
            "subgenres": []
        },
        {
            "id": 39,
            "name": "Pop / Rock",
            "slug": "pop-rock",
            "subgenres": []
        },
        {
            "id": 15,
            "name": "Progressive House",
            "slug": "progressive-house",
            "subgenres": []
        },
        {
            "id": 13,
            "name": "Psy-Trance",
            "slug": "psy-trance",
            "subgenres": []
        },
        {
            "id": 41,
            "name": "Reggae / Dub",
            "slug": "reggae-dub",
            "subgenres": []
        },
        {
            "id": 11,
            "name": "Tech House",
            "slug": "tech-house",
            "subgenres": []
        },
        {
            "id": 6,
            "name": "Techno",
            "slug": "techno",
            "subgenres": []
        },
        {
            "id": 7,
            "name": "Trance",
            "slug": "trance",
            "subgenres": []
        }
    ]
}

还有我的课程

    public class Genres
    {
        public Metadata metadata { get; set; }
        public List<Result> results { get; set; }
    }

    public class Metadata
    {
        public int page {get; set; }
        public int perPage { get; set;}
        public int count { get; set; }
    }

    public class Result
    {
        public int id { get; set; }
        public string name { get; set; }
        public string slug { get; set; }
        public List<object> subgenres { get; set; }
    }

我的代码使用json.net反序列化数据.

And my code to deserialize the data using json.net.

 void beatportTest_GetDataCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        try
        {
            List<Result> data = JsonConvert.DeserializeObject<List<Result>>(e.Result);

            //foreach loop to display data

我希望能够显示Results类中每种流派的名称,但是在我的应用程序编译时出现错误.

I want to be able to display the name of each genre from the Results class, but i get the error when my app compiles.

推荐答案

您的JSON数据具有两个主要元素 metadata results .并且根据您的类结构,流派类也具有相同的结构.但是在您的代码中,您尝试将结构反序列化为 Results ,这就是您收到错误的原因.您应该尝试将序列化反序列化为 Genres 类.
新代码将类似于

Your JSON data has two main elements metadata and results. And according to you class structure, the Genres class also has the same structure. But in your code you are trying to de-serialize the structure to Results, thats why you are getting the error.
You should try to de-serialize to Genres class.
The new code will be something like


void beatportTest_GetDataCompleted(object sender, DownloadStringCompletedEventArgs e)
{
   try
   {
      Genres data = JsonConvert.DeserializeObject(e.Result);
      // for-each loop to display data
   }
   catch(Exception ex)
   {
   }
}

这篇关于无法将JSON对象反序列化为类型'System.Collections.Generic.List'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆