objective-c - 在 Mac 上使用 NSJSONSerialization

标签 objective-c macos twitter nsjsonserialization

我正在尝试在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/

相关文章:

python - 在 OSX 中调用 pydoc - 链接到底是如何工作的?

cocoa - Cocoa (Mac) 中的 HitTest 文本?

cocoa - 无法为自定义 View 设置动画

twitter - 按国家/地区获取顶级推特趋势

iphone - 在 5X 表中将一个整数四舍五入到最接近的值?

iphone - 用户交互后刷新屏幕

IOS 使用 CGSize 弃用方法创建文本大小

objective-c - 如何添加像照片应用程序这样的动画

iphone - Twitter API(从 ID 到屏幕名称)

python - Tweepy rate_limit_status-如何获取剩余的推文