Cannot deserialize JSON object into type 'System.Collections.Generic.List'

拈花ヽ惹草 提交于 2019-12-13 07:16:49

问题


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": []
        }
    ]
}

And my classes

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

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

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.


回答1:


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)
   {
   }
}


来源:https://stackoverflow.com/questions/9058656/cannot-deserialize-json-object-into-type-system-collections-generic-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!