0

我从https://twitter.com/statuses/public_timeline.json获取 JSON 对象

所以,我的代码是下一个:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:@"https://twitter.com/statuses/public_timeline.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];


}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    students = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
     for(NSDictionary *item in students) {
        NSLog(@"Message: %@", [item objectForKey:@"message"]);
     }
}

所以,我得到下一个错误:

2013-01-23 21:42:02.672 students[94907:11603] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7471580
2013-01-23 21:42:02.681 students[94907:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7471580'
4

3 回答 3

0

JSON 将 *item 创建为 NSString。而不是 NSLog(...[item objectForKey:@"message"],试着NSLog(@"%@", item);看看它是什么。也许它已经是你想要的消息了

于 2013-01-23T21:43:29.803 回答
0

这意味着您正在从一个 nil 对象中调用 objectForKey:,您应该尝试以下操作将数据下载到一个字符串中,然后将该字符串传递给 NSJSONSerializer,迭代

NSMutableString *responseData = [[NSMutableString alloc] init];

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:@"https://twitter.com/statuses/public_timeline.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];


}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
        NSLog(@"we got a response ");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
        NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [responseData appendString:response];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSDictionary *students = [NSJSONSerialization JSONObjectWithData:[NSData dataWithBytes:[responseData UTF8String]
                                                                                    length:[responseData lengthOfBytesUsingEncoding:NSUTF8StringEncoding]]
                                                             options:0
                                                               error:nil];
    for(NSDictionary *item in students) {
        NSLog(@"Message: %@", [item objectForKey:@"message"]);
    }
}
于 2013-01-24T00:03:43.163 回答
0

试试这个我希望这会帮助你..

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

     mData = [[NSMutableData alloc] init];
     [mData appendData:data];
}



-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

      NSDictionary *students = [NSJSONSerialization JSONObjectWithData: mData options:kNilOptions error:nil];

     for(NSDictionary *item in students) {
            NSLog(@"Message: %@", item);
        }
     NSLog(@"NSDictionary: %@", students);
 }
于 2013-03-05T06:15:23.630 回答