1

我正在尝试解析这样的json,其中使用了许多数组和对象如何解析它。

[
    {
        "MusicData": [
            {
                "user_music_id": "199",
                "music_id": "2",
                "music_name": "Country"
            },
            {
                "user_music_id": "200",
                "music_id": "2",
                "music_name": "Country"
            }
        ]
    },
    {
        "SportData": [
            {
                "user_sport_id": "179",
                "sport_id": "4",
                "sport_name": "Hockey"
            }
        ]
    },
    {
        "HobbyData": []
    },
    {
        "RelationData": []
    },
    {
        "MovieData": [
            {
                "user_movie_id": "144",
                "movie_id": "6",
                "movie_name": "Drama"
            }
        ]
    },
    {
        "BookData": []
    },
    {
        "CarrerData": [
            {
                "user_carrer_id": "186",
                "carrer_id": "7",
                "carrer_name": "Marketing"
            },
            {
                "user_carrer_id": "187",
                "carrer_id": "8",
                "carrer_name": "Sales"
            }
        ]
    }
]

MyMusic 是一个字典,它有一个包含许多索引的数组。

如何一一解析其数据?使用标签 MyMusic、SportData 等。

我正在尝试使用以下代码从此 json 解析 CarrerData:

musicTemp._id = [NSString stringWithFormat:@"%@",[[[responseDict objectAtIndex:0] valueForKey:@"MusicData"] valueForKey:@"user_music_id"]];
4

1 回答 1

1

似乎您正在跳过一个数组,请尝试:

musicTemp._id = [NSString stringWithFormat:@"%@",[[[[responseDict objectAtIndex:0] valueForKey:@"MusicData"] objectAtIndex:0] valueForKey:@"user_music_id"]];`

" {}" 代表字典
" []" 代表数组

因此,要迭代您可以执行的对象:

 NSArray* array=[[responseDict objectAtIndex:0] valueForKey:@"MusicData"]; 
   for (NSDictionary *dict in array) {
        NSString *userMusicID=[dict objectForKey:@"user_music_id"];
        NSString *musicID=[dict objectForKey:@"music_id"];
        //etc 
    }

您可以对其他元素执行相同的操作。

于 2012-11-28T15:17:10.877 回答