我正在使用SZJsonParser从站点下载JSON对象。 这是我的代码:
#import <Foundation/Foundation.h>
#import "SZJsonParser.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
// insert code here...
NSURL *url = [[NSURL alloc] initWithString:@"http://mymovieapi.com/?title=Cars"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSArray *arr = [str jsonObject];
for (int i = 0; i < [arr count]; i ++)
{
NSLog(@"%@",[arr objectAtIndex:i]);
}
}
return 0;
}
当我运行终端应用程序时,这是输出:
2013-09-19 15:54:49.957 SimpleJSONTerminal[24160:303] {
actors = (
"Owen Wilson",
"Paul Newman",
"Bonnie Hunt",
"Larry the Cable Guy",
"Cheech Marin",
"Tony Shalhoub",
"Guido Quaroni",
"Jenifer Lewis",
"Paul Dooley",
"Michael Wallis",
"George Carlin",
"Katherine Helmond",
"John Ratzenberger",
"Joe Ranft",
"Michael Keaton"
);
"also_known_as" = (
"Cars - Quatre roues"
);
country = (
USA
);
directors = (
"John Lasseter",
"Joe Ranft"
);
genres = (
Animation,
Adventure,
Comedy,
Family,
Sport
);
"imdb_id" = tt0317219;
"imdb_url" = "http://www.imdb.com/title/tt0317219/";
language = (
English,
Italian,
Japanese,
Yiddish
);
"plot_simple" = "A hot-shot race-car named Lightning McQueen gets waylaid in Radiator Springs, where he finds the true meaning of friendship and family.";
poster = {
cover = "http://imdb-poster.b0.upaiyun.com/000/317/219.jpg!cover?_upt=65f114a91379629498";
imdb = "http://ia.media-imdb.com/images/M/MV5BMTE5Mzk5MTA2Ml5BMl5BanBnXkFtZTYwNTY3NTc2._V1_SY317_CR0,0,214,317_.jpg";
};
rating = "7.3";
"rating_count" = 150431;
"release_date" = 20060822;
runtime = (
"117 min"
);
title = "Cars - Quatre roues\n \"Cars\"";
type = VG;
writers = (
"John Lasseter",
"Joe Ranft"
);
year = 2006;
}
如何将其存储在NSDictionary中,以便我可以将“title”用作Key,然后获取信息?
谢谢
答案 0 :(得分:0)
NSData *theData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:jsonFileName ofType:@"json"]];
NSDictionary *myDictionary = [NSJSONSerialization JSONObjectWithData:theData options:NSJSONReadingMutableContainers error:nil];
希望这会对您有所帮助。
答案 1 :(得分:0)
NSData *jsonData = [content dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *result = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&err] : nil;
if(err)
{
NSLog(@"JSON Failed. Error - %@ %@",
[err localizedDescription],
[[err userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
else
{
// Do what you have to do with your data
}
答案 2 :(得分:0)
Usethis........
NSURL *url = [[NSURL alloc] initWithString:@"http://mymovieapi.com/?title=Cars"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSPropertyListSerialization
propertyListWithData:[str dataUsingEncoding:NSUTF8StringEncoding]
options:kNilOptions
format:NULL
error:NULL];
NSLog(@"dict:%@",dict);