我是一名新的iOS开发人员,他有一个大型应用程序要编写。
首先,我们有一台服务器,杰森在哪里。我必须从该服务器获取数据并进行解析。并在显示的各个位置显示数据,例如UIView。
但是,问题是,我可以连接到服务器,获取数据并进行解析,但无法在括号中显示数据。
让我解释:

我们的JSON数据是这样的:

{
"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"
                }
        ]
    }

}
}


我想获取objectForKey:@"hls",但不能。这简直让我讨厌JSON中的所有数据。我的代码如下所示:

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


请帮忙...

最佳答案

您没有正确获取streams数组。请尝试以下操作:

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

关于ios - Objective-C JSON数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17771085/

10-10 21:08
查看更多