久しぶりに、Objective-C の復習。
JSONの解析には、最近は標準ライブラリの、NSJSONSerialization を使用するらしい。
[c]
//URL生成
NSURL *url = [NSURL URLWithString:@"http://server.host/users.json"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//HTTPメソッド設定
[request setHTTPMethod:@"GET"];
//リクエスト送信
NSHTTPURLResponse *response;
NSError *error = nil;
NSData *jsonData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if(!error)
{
//JSON解析
NSLog(@"status code: %d", [response statusCode]);
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingAllowFragments
error:&error];
for (NSDictionary *dict in jsonDict)
{
NSLog(@"%@ %@", [dict objectForKey:@"last_name"],[dict objectForKey:@"first_name"]);
}
}else{
NSLog(@"error: %@", error);
}
[/c]