如何处理来自NSDictionary的json响应

时间:2014-06-11 06:53:44

标签: ios json

我已经解析了一个json url并将json结果保存在NSDictionary结果中。我在字典中解析的结果是这样的:

 result : >>>>>>> {
        videos =     (
                    {
                title = "1";
                videolink = "http://dittotv.cdn.bitgravity.com/cdn-_definst_v/secureplaylist.m3u8?e=1402470360&a=IN&h=1f7eba1cb9103525741581eac8e9347c";
            },
                    {
                title = "2";
_definst_v/secureplaylist.m3u8?e=1402470360&a=IN&h=98fc70f984c2624c4f2ba09ba017a7b6";
            },
                    {
                title = "3";
                videolink = "http://dittotv.cdn.bitgravity.com/cdn-_definst_/secureplaylist.m3u8?e=1402470360&a=IN&h=23ee1c3ede6088a50c565aa734439818";
            },
                    {
                title = "4";
                videolink = "http://dittotv.cdn.bitgravity.com/cdn-_definst_/secureplaylist.m3u8?e=1402470360&a=IN&h=b4c400342b59639ad69f837033e66dc1";
            }
        );
    }

现在我希望标题4的视频链接在Web视图中打开。 如何在某些字符串中获取该视频链接,以便我可以将该字符串传递给Web视图。

代码是:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSError *myError = nil;

   NSDictionary *results=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];

    if (data != nil) {


        NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"response is %@",jsonString);

        NSLog(@"result : >>>>>>> %@", results);
        if([results objectForKey:@" videolink"])
        {
            [singleton sharedobject].urlid = [results objectForKey:@"videolink"];

            NSLog(@"userid: %@",[singleton sharedobject].urlid);
        }

    }
}

2 个答案:

答案 0 :(得分:1)

id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *status = [[[[json valueForKeyPath:@"videos"] objectAtIndex:3] valueForKey:@"videolink"] stringValue];

答案 1 :(得分:-1)

如果你的NSDictionary变量名为videos,那么访问第4个元素就像这样简单:

[(NSArray*)videos objectAtIndex:3];// will return a smaller dictionary of the superset

如果要访问第4个索引处的属性videolink

[[(NSArray*)videos objectAtIndex:3] valueForKey:@"videolink"]; // will return a string

这段代码可以预先知道您的字典结构(键名等)。