无法将JSON对象反序列化为“System.Collections.Generic.List”类型

时间:2012-01-30 02:21:12

标签: c# json windows-phone-7 json.net

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


{
    "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反序列化数据。

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

            //foreach loop to display data

我希望能够从Results类中显示每个类型的名称,但是当我的应用程序编译时,我会收到错误。

1 个答案:

答案 0 :(得分:2)

您的JSON数据有两个主要元素元数据结果。根据你的类结构,类型类也具有相同的结构。但是在您的代码中,您尝试将结构反序列化为结果,这就是您收到错误的原因。您应该尝试反序列化为类型 class。
新代码将是类似的


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