将JSON读取到NSString并获取特定条目

时间:2012-10-10 09:25:24

标签: objective-c ios json sbjson

我遇到了SBJSON Parser的问题。我正在尝试从一个网站获取一些信息,这些信息会返回一个json文件。然后我将其解析为字符串,然后解析为字典(工作正常)。但是,当我想要该字典中的最后一个元素(名称)时,它只会给我一个错误。这是我的代码:

SBJsonParser *parser = [[SBJsonParser alloc] init];

self.videoId = videoId;
// Grab the contents from the url and save it in a string
NSString *content = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://gdata.youtube.com/feeds/api/videos/%@?alt=json",videoId]] encoding:NSUTF8StringEncoding error:nil];
// Save the information from the string in a dict
NSDictionary *dict = [parser objectWithString:content error:nil];

NSArray *string = [[dict objectForKey:@"entry"] objectForKey:@"author"];

NSLog(@"%@", string);

这是输出:

2012-10-10 11:11:47.731 quoteGen[13862:c07] (
    {
    name =         {
        "$t" = CommanderKrieger;
    };
    uri =         {
        "$t" = "http://gdata.youtube.com/feeds/api/users/CommanderKrieger";
    };
}

我如何获得名称或uri?

1 个答案:

答案 0 :(得分:0)

NSString *name = [[[string objectAtIndex:0] objectForKey:@"name"] objectForKey:@"$t"];
 NSString *urlString = [[[string objectAtIndex:0] objectForKey:@"uri"] objectForKey:@"$t"];

我认为这会对你有所帮助。