Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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];

}

share|improve this question
    
The [..] 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:38
    
Just get the first stream from your array of streams instead of looping over it. – talnicolas Jul 22 '13 at 19:39
2  
(Hint: Learn what an array is. Don't just copy code from somewhere.) – Hot Licks Jul 22 '13 at 19:41

1 Answer 1

up vote 0 down vote accepted

The problem is your for loop. There are two stream objects in "streams" as indicated by the [] in the JSON object, meaning it's an array, and it is populated with two values. You are iterating over both of these objects, and always getting the second one. Select which object you want manually, instead of iterating over them, and automatically getting yourself stuck with whichever value is last.

Instead of this:

for (NSDictionary *stream in streams)
{

    NSString *str = [[NSString alloc]initWithString:[stream valueForKey:@"hls"]];
    videoURL = [NSURL URLWithString:str];
}

You probably want this:

NSArray *arrayOfStreams = [json valueForKeyPath:@"mbsServer.channel.streams"];
NSDictionary *stream = [arrayOfStreams objectAtIndex:0];
NSString *str = [[NSString alloc]initWithString:[stream valueForKey:@"hls"]];
videoURL = [NSURL URLWithString:str];

Helpful:

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html

share|improve this answer
    
How can I do it? The HLS is a dynamic value, which is changing every 2 hours, it is getting its data from Wowza server. So I need to just get the first one every time. Second one is not active yet. Can u just write an exmplae – Zaur Bilalov Jul 22 '13 at 19:43
    
See my edits for an example. I modified it slightly, to be a little more verbose, but clearer as to what was going on, and therefore what you were doing wrong. – ChrisCM Jul 22 '13 at 19:43
    
It did work. Thank you very much. I understood what I was doing wrong :) – Zaur Bilalov Jul 22 '13 at 19:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.