CodeGuru CodeGuru - 5 months ago 9
iOS Question

Retrieve a data from json ios

This is the content of dictionary *data

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>";
}
);
};
}


This is the response i am getting from json, what logic should i write to retrieve "VideoId" linked to a specific "Speciality" (for ex- Speciality = 22)

if i write following code, i am getting all @"VideoId"

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

Answer

Use this code,

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

subArray is get all the videos values, then you get single value use this code,

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

or you will get all videoID in single variable use this below code,

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

finally tempArray get All the VideoID values, anywhere you use,

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

hope its helpful