2011-09-30 26 views
2

我得到我的JSON一個使用未聲明的標識符錯誤的,但我下面從http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c使用未聲明的標識符的JSON的iOS

我怎麼解決這個問題的例子嗎?是的,我是很新的Objective-C的\ IOS :)謝謝

我把在我看來,基於應用程序的代碼在我viewcontroller.m文件

的問題是與「SBJSON *解析器= [[SBJSON alloc] init];「

// Create new SBJSON parser object 
    SBJSON *parser = [[SBJSON alloc] init]; 

// Prepare URL request to download statuses from Twitter 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json"]]; 

// Perform request and get JSON back as a NSData object 
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

// Get JSON as a NSString from NSData response 
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 

// parse the JSON response into an object 
// Here we're using NSArray since we're parsing an array of JSON status objects 
NSArray *statuses = [parser objectWithString:json_string error:nil]; 

// Each element in statuses is a single status 
// represented as a NSDictionary 
for (NSDictionary *status in statuses) 
{ 
    // You can retrieve individual values using objectForKey on the status NSDictionary 
    // This will print the tweet and username to the console 
    NSLog(@"%@ - %@", [status objectForKey:@"text"], [[status objectForKey:@"user"] objectForKey:@"screen_name"]); 
} 
+1

你有沒有想起導入SBJSON庫? – Circumflex

+0

是的。我添加了來自classes文件夾和#import SBJson.h的所有文件。所以不知道發生了什麼。 – Rick

回答

4

嘗試改變線SBJsonParser *parser = [[SBJsonParser alloc] init];

編輯:

的方式,可以更容易是使用的NSString的類別,SBJSON提供:

NSArray *statuses = (NSArray *)[json_string JSONValue]; 
+0

感謝您的工作,現在我試圖修改代碼到我的網址,並從數組中打印一個項目。 NSLog(@「Message:%@」,[status objectForKey:@「message」]);工作? – Rick

+0

你知道我應該真的開始一個新的問題的新線程。 – Rick