从json ios中检索数据

时间:2016-06-13 10:24:18

标签: ios objective-c json nsdictionary

这是字典*数据的内容

 Data =     {
        Specialities =         (
                        {
                ApprovalStatus = Unapproved;
                CurrencyCode = "<null>";
                Packages = "<null>";
                Photos = "<null>";
                ServiceDescription = 356563;
                Speciality = 22;
                SpecialityName = "Wedding Photographer";
                UserFRPs = "<null>";
                Videos =                 (
                                        {
                        VideoId = TWhSjpsGvPQ;
                        VideoLink = "http://www.youtube.com/watch?v=TWhSjpsGvPQ";
                        VideoType = YouTube;
                    },
                                        {
                        VideoId = N2CJrhHEydA;
                        VideoLink = "http://www.youtube.com/watch?v=N2CJrhHEydA";
                        VideoType = YouTube;
                    },
                                        {
                        VideoId = Lq6faQVYcwY;
                        VideoLink = "http://www.youtube.com/watch?v=Lq6faQVYcwY";
                        VideoType = YouTube;
                    },
                                        {
                        VideoId = v8WjMiodcKo;
                        VideoLink = "http://www.youtube.com/watch?v=v8WjMiodcKo";
                        VideoType = YouTube;
                    }
                );
            },
                        {
                ApprovalStatus = Unapproved;
                CurrencyCode = "<null>";
                Packages = "<null>";
                Photos = "<null>";
                ServiceDescription = 52454353;
                Speciality = 37;
                SpecialityName = "Hair and Makeup Stylist";
                UserFRPs = "<null>";
                Videos = "<null>";
            },
                        {
                ApprovalStatus = Unapproved;
                CurrencyCode = "<null>";
                Packages = "<null>";
                Photos = "<null>";
                ServiceDescription = 21212;
                Speciality = 55;
                SpecialityName = Transport;
                UserFRPs = "<null>";
                Videos = "<null>";
            }
        );
    };
}

这是我从json得到的回应,我应该写什么逻辑来检索&#34; VideoId&#34;与特定的&#34;专业&#34; (对于ex- Specialty = 22)

如果我写下面的代码,我会得到所有@&#34; VideoId&#34;

[[[[responseDictionary valueForKey:@"Data"]valueForKey:@"Specialities"]valueForKey:@"Videos"]valueForKey:@"VideoId"]

2 个答案:

答案 0 :(得分:0)

解析方式如下:

        NSDictionary *dataDict = [responseDictionary valueForKey:@"Data"];
        NSMutableArray *specialityArray = [dataDict valueForKey:@"Specialities"];
        NSDictionary *objectDict = [specialityArray objectAtIndex:your index (0/1/2)];
        int *speciality = [[objectDict ValueForKey:@"Speciality"] intValue];
    if(speciality = 22)
    {
        NSMutableArray *videoArray = [objectDict valueForKey:@"Videos"];
        NSDictionary *videoObject = [videoArray objectAtIndex:your index (0/1/2)];
        NSString     *videoID     = [videoObject valueForKey:@"VideoId"];
    }

根据需要相应地解析其余部分。

答案 1 :(得分:0)

使用此代码,

    NSArray *wholeArray = [[responseDictionary objectForKey:@"Data"] valueForKey:@"Specialities"];
    NSArray *subArray = [[wholeArray objectAtIndex:0] valueForKey:@"Videos"];

subArray获取所有视频值,然后使用此代码获得单值,

NSString  *videoID = [[subArray objectAtIndex:i] valueForKey:@"VideoId"];

或者你将获得单个变量中的所有videoID使用下面的代码,

NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < subArray.count; i++) {
        [tempArray addObject:[[subArray objectAtIndex:i] valueForKey:@"VideoId"]];
    }

最后,tempArray会在您使用的任何地方获取所有VideoID值,

NSString  *videoID = [tempArray objectAtIndex:0/1/2];

希望它有用