简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM