用了很多方法的都沒有返回結果
先看一下代碼:
json文件:
{ "speakers" :
[
{
"name":"Value",
"picture": "URL VALUE",
"business":"VALUE",
"desc":"VALUE",
"twitter": "URL VALUE"
}
{
...
}
]
}
開始在一個dictionary
中保存speaker屬性
這個包含一個數組,有一些name,business屬性
objective代碼:
NSString *URLStr = @"URLofMyJsonFile";
NSURLRequest *JSONRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithString:URLStr ]]];
NSData *JSONData = [NSURLConnection sendSynchronousRequest:JSONRequest returningResponse:nil error:nil];
NSError *parsingError = nil;
NSDictionary *speakerDictionnary = [NSJSONSerialization JSONObjectWithData:JSONData options:0 error:&parsingError];
NSArray *speakersArray = [speakerDictionnary objectForKey:@"news"];
for (NSDictionary *oneSpeaker in speakersArray) {
NSLog(@"The speakers's name is %@", [oneSpeaker objectForKey:@"name"]);
NSLog(@"The speakers's business is %@", [oneSpeaker objectForKey:@"business"]);
NSLog(@"The speakers's desc is %@", [oneSpeaker objectForKey:@"desc"]);
}
在dictionary之間需要逗號。
{ "speakers" :
[
{
"name":"Value",
"picture": "URL VALUE",
"business":"VALUE",
"desc":"VALUE",
"twitter": "URL VALUE"
} <=== MISSING COMMA HERE
{
...
}
]
}