我正在尝试在Mac应用程序中使用NSJSONSerialization
,但我无法让它工作,我正在尝试让它加载UserTimeline,并且只显示最后一个的文本推文,就是这样,但我似乎找不到正确的使用方法。我尝试使用的API:
http://api.twitter.com/1/statuses/user_timeline.json?screen_name=AlexTrott_
还有
http://twitter.com/statuses/user_timeline/AlexTrott_.json
但是我没有看过,这就是我一直在尝试使用 NSJSONSerialization
的方式:
NSString *tweets = [NSURL URLWithString:@"http://twitter.com/statuses/user_timeline/AlexTrott_.json"];
NSData *jsonData = [tweets dataUsingEncoding:NSUTF8StringEncoding];
NSError *e = nil;
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e];
NSLog(@"%@", json);
这正是我一直用来查看是否已加载它们的方法,但它一直让我失望。
我也尝试过要尝试这个方法http://www.raywenderlich.com/5492/working-with-json-in-ios-5不过这仅适用于 iOS,我不知道如何让它在 Mac 上工作。
任何有关如何在 mac 上使用 NSJSONSerialization
的链接都很棒,或者任何帮助也将非常感谢。我尝试在苹果开发者论坛上寻求帮助,但没有成功。
最佳答案
NSURL *tweets = [NSURL URLWithString:@"http://twitter.com/statuses/user_timeline/AlexTrott_.json"];
//NSData *jsonData = [tweets dataUsingEncoding:NSUTF8StringEncoding];
//NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e];
NSData *jsonData=[NSData dataWithContentsOfURL:tweets];
NSError *e = nil;
id yourJsonData=[NSJSONSerialization JSONObjectWithData:jsonData options:
NSJSONReadingMutableContainers error:&error];
NSLog("%@",yourJsonData );//you must get a json object here
//let's assume you need to get the key kalled user_name from your JSON Response
NSDictionary *theJson=[yourJsonData objectForKey:@"user_name"];
for(NSMutableArray *usernames in theJson){
// do something with usernames from your object
}
关于objective-c - 在 Mac 上使用 NSJSONSerialization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9453727/