如何在iOS 7中解析Json Data?

时间:2014-09-07 07:44:59

标签: json parsing ios7

我当前的Json数据是这样的:

{
    resultCount = 12;
    results =     (
                {
            amgArtistId = 468749;
            artistId = 909253;
            artistLinkUrl = "https://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4";
            artistName = "Jack Johnson";
            artistType = Artist;
            primaryGenreId = 21;
            primaryGenreName = Rock;
            radioStationUrl = "https://itunes.apple.com/station/idra.909253";
            wrapperType = artist;
        },
                {
            amgArtistId = 468749;
            artistId = 909253;
            artistName = "Jack Johnson";
            artistViewUrl = "https://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4";
            artworkUrl100 = "http://a4.mzstatic.com/us/r30/Music4/v4/41/df/6f/41df6fb5-d08f-5573-fb4b-a56a9b6ea0cb/UMG_cvrart_00602537868858_01_RGB72_900x810_06UMGIM25847.100x100-75.jpg";
            artworkUrl60 = "http://a1.mzstatic.com/us/r30/Music4/v4/41/df/6f/41df6fb5-d08f-5573-fb4b-a56a9b6ea0cb/UMG_cvrart_00602537868858_01_RGB72_900x810_06UMGIM25847.60x60-50.jpg";
            collectionCensoredName = "In Between Dreams";
            collectionExplicitness = notExplicit;
            collectionId = 879273552;
            collectionName = "In Between Dreams";
            collectionPrice = "9.99";
            collectionType = Album;
            collectionViewUrl = "https://itunes.apple.com/us/album/in-between-dreams/id879273552?uo=4";
            copyright = "\U00e2\U0084\U0097 2013 Jack Johnson";
            country = USA;
            currency = USD;
            primaryGenreName = Rock;
            releaseDate = "2014-05-27T07:00:00Z";
            trackCount = 16;
            wrapperType = collection;
        },
                {
            amgArtistId = 468749;
            artistId = 909253;
            artistName = "Jack Johnson";
            artistViewUrl = "https://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4";
            artworkUrl100 = "http://a1.mzstatic.com/us/r30/Music4/v4/4d/b0/3e/4db03e7a-b4ee-a50d-8119-3a4fdbc0bdac/UMG_cvrart_00602537869640_01_RGB72_900x829_07UMGIM18925.100x100-75.jpg";
            artworkUrl60 = "http://a1.mzstatic.com/us/r30/Music4/v4/4d/b0/3e/4db03e7a-b4ee-a50d-8119-3a4fdbc0bdac/UMG_cvrart_00602537869640_01_RGB72_900x829_07UMGIM18925.60x60-50.jpg";
            collectionCensoredName = "Sleep Through the Static";
            collectionExplicitness = notExplicit;
            collectionId = 879716730;
            collectionName = "Sleep Through the Static";
            collectionPrice = "9.99";
            collectionType = Album;
            collectionViewUrl = "https://itunes.apple.com/us/album/sleep-through-the-static/id879716730?uo=4";
            copyright = "\U00e2\U0084\U0097 2014 Jack Johnson";
            country = USA;
            currency = USD;
            primaryGenreName = Rock;
            releaseDate = "2014-05-27T07:00:00Z";
            trackCount = 15;
            wrapperType = collection;
        },
                {
            amgArtistId = 468749;
            artistId = 909253;
            artistName = "Jack Johnson";
            artistViewUrl = "https://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4";
            artworkUrl100 = "http://a1.mzstatic.com/us/r30/Music/v4/4e/55/df/4e55dfbc-bc28-b5c9-0d4a-66d2d05cfa96/UMG_cvrart_00602537869671_01_RGB72_1480x1480_10UMGIM09953.100x100-75.jpg";
            artworkUrl60 = "http://a5.mzstatic.com/us/r30/Music/v4/4e/55/df/4e55dfbc-bc28-b5c9-0d4a-66d2d05cfa96/UMG_cvrart_00602537869671_01_RGB72_1480x1480_10UMGIM09953.60x60-50.jpg";
            collectionCensoredName = "To the Sea";
            collectionExplicitness = notExplicit;
            collectionId = 879206883;
            collectionName = "To the Sea";
            collectionPrice = "9.99";
            collectionType = Album;
            collectionViewUrl = "https://itunes.apple.com/us/album/to-the-sea/id879206883?uo=4";
            copyright = "\U00e2\U0084\U0097 2014 Jack Johnson";
            country = USA;
            currency = USD;
            primaryGenreName = Rock;
            releaseDate = "2014-05-27T07:00:00Z";
            trackCount = 15;
            wrapperType = collection;
        },
                {
            amgArtistId = 468749;
            artistId = 909253;
            artistName = "Jack Johnson";
            artistViewUrl = "https://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4";
            artworkUrl100 = "http://a4.mzstatic.com/us/r30/Features4/v4/e8/51/40/e8514089-6736-bf56-74f9-5cff860906f6/dj.onebzxvm.100x100-75.jpg";
            artworkUrl60 = "http://a4.mzstatic.com/us/r30/Features4/v4/e8/51/40/e8514089-6736-bf56-74f9-5cff860906f6/dj.onebzxvm.60x60-50.jpg";
            collectionCensoredName = "From Here to Now to You";
            collectionExplicitness = notExplicit;
            collectionId = 659234734;
            collectionName = "From Here to Now to You";
            collectionPrice = "9.99";
            collectionType = Album;
            collectionViewUrl = "https://itunes.apple.com/us/album/from-here-to-now-to-you/id659234734?uo=4";
            copyright = "\U00e2\U0084\U0097 2013 Jack Johnson";
            country = USA;
            currency = USD;
            primaryGenreName = Rock;
            releaseDate = "2013-09-17T07:00:00Z";
            trackCount = 12;
            wrapperType = collection;
        },
                {
            amgArtistId = 468749;
            artistId = 909253;
            artistName = "Jack Johnson";
            artistViewUrl = "https://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4";
            artworkUrl100 = "http://a4.mzstatic.com/us/r30/Music/1a/34/a1/mzi.clhpvscw.100x100-75.jpg";
            artworkUrl60 = "http://a3.mzstatic.com/us/r30/Music/1a/34/a1/mzi.clhpvscw.60x60-50.jpg";
            collectionCensoredName = "En Concert (Bonus Track Version) [Live]";
            collectionExplicitness = notExplicit;
            collectionId = 333365555;
            collectionName = "En Concert (Bonus Track Version) [Live]";
            collectionPrice = "9.99";
            collectionType = Album;
            collectionViewUrl = "https://itunes.apple.com/us/album/en-concert-bonus-track-version/id333365555?uo=4";
            copyright = "\U00e2\U0084\U0097 2009 Jack Johnson";
            country = USA;
            currency = USD;
            primaryGenreName = Rock;
            releaseDate = "2009-10-26T07:00:00Z";
            trackCount = 21;
            wrapperType = collection;
        },
                {
            amgArtistId = 5723;
            artistId = 78500;
            artistLinkUrl = "https://itunes.apple.com/us/artist/u2/id78500?uo=4";
            artistName = U2;
            artistType = Artist;
            primaryGenreId = 21;
            primaryGenreName = Rock;
            radioStationUrl = "https://itunes.apple.com/station/idra.78500";
            wrapperType = artist;
        },
                {
            amgArtistId = 5723;
            artistId = 78500;
            artistName = U2;
            artistViewUrl = "https://itunes.apple.com/us/artist/u2/id78500?uo=4";
            artworkUrl100 = "http://a4.mzstatic.com/us/r30/Features/64/ca/7a/dj.aibugkmk.100x100-75.jpg";
            artworkUrl60 = "http://a5.mzstatic.com/us/r30/Features/64/ca/7a/dj.aibugkmk.60x60-50.jpg";
            collectionCensoredName = "U218 Singles (Deluxe Version)";
            collectionExplicitness = notExplicit;
            collectionId = 205155692;
            collectionName = "U218 Singles (Deluxe Version)";
            collectionPrice = "14.99";
            collectionType = Album;
            collectionViewUrl = "https://itunes.apple.com/us/album/u218-singles-deluxe-version/id205155692?uo=4";
            copyright = "\U00e2\U0084\U0097 2006 Universal-Island Records Ltd.";
            country = USA;
            currency = USD;
            primaryGenreName = Rock;
            releaseDate = "2006-11-20T08:00:00Z";
            trackCount = 28;
            wrapperType = collection;
        },
                {
            amgArtistId = 5723;
            artistId = 78500;
            artistName = U2;
            artistViewUrl = "https://itunes.apple.com/us/artist/u2/id78500?uo=4";
            artworkUrl100 = "http://a4.mzstatic.com/us/r30/Music/82/b4/94/mzi.wnujigmg.100x100-75.jpg";
            artworkUrl60 = "http://a3.mzstatic.com/us/r30/Music/82/b4/94/mzi.wnujigmg.60x60-50.jpg";
            collectionCensoredName = "No Line On the Horizon";
            collectionExplicitness = notExplicit;
            collectionId = 305352505;
            collectionName = "No Line On the Horizon";
            collectionPrice = "9.99";
            collectionType = Album;
            collectionViewUrl = "https://itunes.apple.com/us/album/no-line-on-the-horizon/id305352505?uo=4";
            copyright = "\U00e2\U0084\U0097 2009 Universal-Island Records Ltd. under exclusive license to Mercury Records Limited in the UK, Interscope Records in the US and Universal Music Group for the rest of the world.";
            country = USA;
            currency = USD;
            primaryGenreName = Rock;
            releaseDate = "2009-03-03T08:00:00Z";
            trackCount = 12;
            wrapperType = collection;
        },
                {
            amgArtistId = 5723;
            artistId = 78500;
            artistName = U2;
            artistViewUrl = "https://itunes.apple.com/us/artist/u2/id78500?uo=4";
            artworkUrl100 = "http://a4.mzstatic.com/us/r30/Features/af/9c/e1/dj.cothgbwe.100x100-75.jpg";
            artworkUrl60 = "http://a3.mzstatic.com/us/r30/Features/af/9c/e1/dj.cothgbwe.60x60-50.jpg";
            collectionCensoredName = "How to Dismantle an Atomic Bomb";
            collectionExplicitness = notExplicit;
            collectionId = 29600233;
            collectionName = "How to Dismantle an Atomic Bomb";
            collectionPrice = "9.99";
            collectionType = Album;
            collectionViewUrl = "https://itunes.apple.com/us/album/how-to-dismantle-atomic-bomb/id29600233?uo=4";
            copyright = "\U00e2\U0084\U0097 2004 Universal-Island Records Ltd.";
            country = USA;
            currency = USD;
            primaryGenreName = Rock;
            releaseDate = "2004-11-22T08:00:00Z";
            trackCount = 11;
            wrapperType = collection;
        },
                {
            amgArtistId = 5723;
            artistId = 78500;
            artistName = U2;
            artistViewUrl = "https://itunes.apple.com/us/artist/u2/id78500?uo=4";
            artworkUrl100 = "http://a1.mzstatic.com/us/r30/Music/v4/94/eb/dc/94ebdc5f-2b8a-c123-0d83-3334c27bc90d/dj.jrlmqsuc.100x100-75.jpg";
            artworkUrl60 = "http://a4.mzstatic.com/us/r30/Music/v4/94/eb/dc/94ebdc5f-2b8a-c123-0d83-3334c27bc90d/dj.jrlmqsuc.60x60-50.jpg";
            collectionCensoredName = "The Joshua Tree (Remastered)";
            collectionExplicitness = notExplicit;
            collectionId = 267826054;
            collectionName = "The Joshua Tree (Remastered)";
            collectionPrice = "9.99";
            collectionType = Album;
            collectionViewUrl = "https://itunes.apple.com/us/album/the-joshua-tree-remastered/id267826054?uo=4";
            copyright = "\U00e2\U0084\U0097 2007 Universal-Island Records Ltd. under exclusive licence to Mercury Records Limited";
            country = USA;
            currency = USD;
            primaryGenreName = Rock;
            releaseDate = "2007-11-20T08:00:00Z";
            trackCount = 11;
            wrapperType = collection;
        },
                {
            amgArtistId = 5723;
            artistId = 78500;
            artistName = U2;
            artistViewUrl = "https://itunes.apple.com/us/artist/u2/id78500?uo=4";
            artworkUrl100 = "http://a1.mzstatic.com/us/r30/Music/b6/b4/df/mzi.rpwpimbq.100x100-75.jpg";
            artworkUrl60 = "http://a1.mzstatic.com/us/r30/Music/b6/b4/df/mzi.rpwpimbq.60x60-50.jpg";
            collectionCensoredName = "The Best of 1980-1990";
            collectionExplicitness = notExplicit;
            collectionId = 280046709;
            collectionName = "The Best of 1980-1990";
            collectionPrice = "9.99";
            collectionType = Album;
            collectionViewUrl = "https://itunes.apple.com/us/album/the-best-of-1980-1990/id280046709?uo=4";
            copyright = "\U00e2\U0084\U0097 1998 Universal-Island Records Ltd.";
            country = USA;
            currency = USD;
            primaryGenreName = Rock;
            releaseDate = "2008-05-13T07:00:00Z";
            trackCount = 15;
            wrapperType = collection;
        }
    );
}

如何使用NSJSONSerialization解析此数据?

1 个答案:

答案 0 :(得分:1)

NSError *jsonParsingError = nil;
NSDictionary *parsedJson = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

现在,您可以使用json响应中的键来访问json元素。 例如,要计算结果。

[parsedJson objectForKey:@"resultCount"];

<强>更新 在你的json字符串中,结果是一个数组。所以在序列化后你会得到一系列字典。因此,要获得像'amgArtistId'这样的属性,首先必须从字典中获取结果键的值,该字典将包含一个(字典)数组。然后,您可以使用索引循环该数组或访问单个词典。

NSArray *results = [parsedJson objectForKey:@"results"];
NSString *amgArtistId = [[results firstObject] objectForKey:@"amgArtistId"];

这样你也可以获得其他元素。您不必为此序列化每个元素。序列化json字符串就足够了。