Zaur Bilalov
Zaur Bilalov

Reputation: 107

Objective-C JSON Data

I am a new iOS Developer, who has a big app to write. First we have a server where is the jason. I have to get data from that server and parse it. And display the data in various places on display like UIView. But, the problem is, I can connect to server, get data, and parse it, but i cannot display data in brackets. Let me explain:

Our JSON Data is this:

{
"mbsServer": {
    "version": 1,
    "serverTime": 1374400122,
    "status": 2000,
    "subscriptionExpireTime": 1575057600,
    "channel": {
        "id" : 47,
        "name" : "Yurd TV",
        "logo" : "XXX.png",
        "screenshot" : "screen.png",
        "packageId" : 0,
        "viewers": 1,
        "access": true,
        "streams" : [
                {
                    "birate" : 200,
                    "hls"  : "xxxx.m2u8",
                    "rtsp" : "RTSP.xx"
                },
                {
                    "birate" : 500,
                    "hls"  : "xxxxx",
                    "rtsp" : "xxxxx"
                }
        ]
    }

}
}    

I want to get objectForKey:@"hls", but I cannot. It is just gicing me the all the data from JSON. My code looks like this:

NSData *JSONData = [[NSData alloc] initWithContentOfURL: [NSURL URLLWithString:@"XXXXXXXXXXX"]];
NSArray *streams = [JSONData objectFromJSONData];
for(NSDictionary *hls in streams)
{
   NSLog(@"%@", [hls objectForKey:@"hls"]);
}

Please help...

Upvotes: 0

Views: 150

Answers (2)

Dominik Hadl
Dominik Hadl

Reputation: 3619

The root object should be an NSDictionary instead of NSArray - or if you want to get the streams array from the JSON, then you are accessing it wrong - see the answer of Alex Skalozub.

Upvotes: 1

Alex Skalozub
Alex Skalozub

Reputation: 2576

You didn't get streams array properly. Try the following:

NSObject *json = [JSONData objectFromJSONData];
NSArray *streams = [json valueForKeyPath:@"mbsServer.channel.streams"];
for (NSDictionary *stream in streams)
{
    NSLog(@"%@", [stream valueForKey:@"hls"]);
}

Upvotes: 1

Related Questions