Reading JSON to NSString and getting a specific entry
-
06-07-2021 - |
سؤال
I have a problem with the SBJSON Parser. I'm trying to get some information from a website which gives back a json file. Then I parse that into a string and then into a dictionary (works fine). But when I want the last element (name) from that dict it just gives me an error. Here's my code:
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);
Here's the output:
2012-10-10 11:11:47.731 quoteGen[13862:c07] (
{
name = {
"$t" = CommanderKrieger;
};
uri = {
"$t" = "http://gdata.youtube.com/feeds/api/users/CommanderKrieger";
};
}
)
How do I get the name or uri?
المحلول
NSString *name = [[[string objectAtIndex:0] objectForKey:@"name"] objectForKey:@"$t"];
NSString *urlString = [[[string objectAtIndex:0] objectForKey:@"uri"] objectForKey:@"$t"];
I think it will be helpful to you.
لا تنتمي إلى StackOverflow