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

これを試してみてください。これが役立つことを願っています..

-(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 に答える
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 に答える