解析复杂的JSON文件时遇到问题

时间:2014-11-27 17:37:15

标签: c# .net json parsing

我正在开发一款使用微软Xbox Music API的应用。在进行搜索查询时,我得到一个JSON字符串,描述可用的专辑,艺术家和歌曲,如下所示:

{
"Artists": {
    "Items": [{
        "Genres": ["Electronic / Dance"],
        "Subgenres": ["Breakbeat / Electro"],
        "Id": "music.7AF41000-0200-11DB-89CA-0019B92A3933",
        "Name": "Hybrid Theory",
        "ImageUrl": "http://musicimage.xboxlive.com/content/music.7AF41000-0200-11DB-89CA-0019B92A3933/image?locale=en-US",
        "Link": "http://music.xbox.com/artist/hybrid-theory/7af41000-0200-11db-89ca-0019b92a3933?partnerID=___",
        "Source": "Catalog"
    },
    {
        "Genres": ["More"],
        "Subgenres": ["Miscellaneous"],
        "Id": "music.A1745200-0200-11DB-89CA-0019B92A3933",
        "Name": "London Ensemble",
        "ImageUrl": "http://musicimage.xboxlive.com/content/music.A1745200-0200-11DB-89CA-0019B92A3933/image?locale=en-US",
        "Link": "http://music.xbox.com/artist/london-ensemble/a1745200-0200-11db-89ca-0019b92a3933?partnerID=___",
        "Source": "Catalog"
    }],
    "TotalItemCount": 2
},
"Albums": {
    "Items": [{
        "ReleaseDate": "2011-01-26T00:00:00Z",
        "Duration": "00:04:26",
        "TrackNumber": 15,
        "IsExplicit": false,
        "Genres": ["Electronic / Dance"],
        "Subgenres": ["Dance"],
        "Rights": ["Purchase",
        "FreeStream",
        "Stream"],
        "Subtitle": "Original Version",
        "Album": {
            "Id": "music.6147B006-0100-11DB-89CA-0019B92A3933",
            "Name": "Suite Lounge - A Luxury & Unique Collection Of Relaxing Lounge Tunes, Vol. 2",
            "ImageUrl": "http://musicimage.xboxlive.com/content/music.6147B006-0100-11DB-89CA-0019B92A3933/image?locale=en-US",
            "Link": "http://music.xbox.com/album/area-42/suite-lounge-a-luxury-unique-collection-of-relaxing-loun/6147b006-0100-11db-89ca-0019b92a3933?partnerID=___",
            "Source": "Catalog"
        },
        "Artists": [{
            "Role": "Main",
            "Artist": {
                "Id": "music.98D90600-0200-11DB-89CA-0019B92A3933",
                "Name": "Area 42",
                "ImageUrl": "http://musicimage.xboxlive.com/content/music.98D90600-0200-11DB-89CA-0019B92A3933/image?locale=en-US",
                "Link": "http://music.xbox.com/artist/area-42/98d90600-0200-11db-89ca-0019b92a3933?partnerID=___",
                "Source": "Catalog"
            }
        }],
        "Id": "music.EF46B006-0100-11DB-89CA-0019B92A3933",
        "Name": "Hybrid Theory",
        "ImageUrl": "http://musicimage.xboxlive.com/content/music.EF46B006-0100-11DB-89CA-0019B92A3933/image?locale=en-US",
        "Link": "http://music.xbox.com/track/area-42/suite-lounge-a-luxury-unique-collection-of-relaxing-loun/hybrid-theory/ef46b006-0100-11db-89ca-0019b92a3933?partnerID=___",
        "Source": "Catalog"
    },
    {
        "ReleaseDate": "2014-02-19T00:00:00Z",
        "Duration": "00:05:30",
        "TrackNumber": 2,
        "IsExplicit": false,
        "Genres": ["Electronic / Dance"],
        "Subgenres": ["Dance"],
        "Rights": ["Purchase",
        "FreeStream",
        "Stream"],
        "Album": {
            "Id": "music.99C82B08-0100-11DB-89CA-0019B92A3933",
            "Name": "The Dark",
            "ImageUrl": "http://musicimage.xboxlive.com/content/music.99C82B08-0100-11DB-89CA-0019B92A3933/image?locale=en-US",
            "Link": "http://music.xbox.com/album/hybrid-theory/the-dark/99c82b08-0100-11db-89ca-0019b92a3933?partnerID=___",
            "Source": "Catalog"
        },
        "Artists": [{
            "Role": "Main",
            "Artist": {
                "Id": "music.7AF41000-0200-11DB-89CA-0019B92A3933",
                "Name": "Hybrid Theory",
                "ImageUrl": "http://musicimage.xboxlive.com/content/music.7AF41000-0200-11DB-89CA-0019B92A3933/image?locale=en-US",
                "Link": "http://music.xbox.com/artist/hybrid-theory/7af41000-0200-11db-89ca-0019b92a3933?partnerID=___",
                "Source": "Catalog"
            }
        }],
        "Id": "music.9BC82B08-0100-11DB-89CA-0019B92A3933",
        "Name": "All Again",
        "ImageUrl": "http://musicimage.xboxlive.com/content/music.9BC82B08-0100-11DB-89CA-0019B92A3933/image?locale=en-US",
        "Link": "http://music.xbox.com/track/hybrid-theory/the-dark/all-again/9bc82b08-0100-11db-89ca-0019b92a3933?partnerID=___",
        "Source": "Catalog"
    }],
    "ContinuationToken": "___",
    "TotalItemCount": 50
}

}

我只有一个专辑的类定义,需要从JSON字符串中获取一个类型为Album的List / Array。但是,我无法弄清楚如何做到这一点。该课程如下:

class Artist
{
    public string Name;
    List<string> genres;
    bool isExplicit;
}

我尝试使用Json.NET,但只能解析值类型而不是引用类型。

0 个答案:

没有答案