How can I get the first "hls" from this json. My source code is searching for value for hls and displaying it. But it gets the second "hls"... JSON Data is:
{
"mbsServer": {
"version": 1,
"serverTime": 1374519337,
"status": 2000,
"subscriptionExpireTime": 1575057600,
"channel": {
"id" : 47,
"name" : "Yurd TV",
"logo" : "XXXX",
"screenshot" : "XXXXXXX",
"packageId" : 0,
"viewers": 1,
"access": true,
"streams" : [
{
"birate" : 200,
"hls" : "XXXXXXXX",
"rtsp" : "XXXXXXX"
},
{
"birate" : 500,
"hls" : "XXXXXXX",
"rtsp" : "XXXXXX"
}
]
}
} }
My code is:
@implementation ViewController - (IBAction)play:(id)sender {
NSData *JSONData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"XXXXXX"]];
NSObject *json = [JSONData objectFromJSONData];
NSArray *streams = [json valueForKeyPath:@"mbsServer.channel.streams"];
for (NSDictionary *stream in streams)
{
NSString *str = [[NSString alloc]initWithString:[stream valueForKey:@"hls"]];
videoURL = [NSURL URLWithString:str];
}
NSURLRequest *req = [NSURLRequest requestWithURL:videoURL];
[_stream loadRequest:req];
}
[..]
indicates an array. Index the array to access the elements of it (which are "objects"). See json.org. – Hot Licks Jul 22 '13 at 19:38streams
instead of looping over it. – talnicolas Jul 22 '13 at 19:39