2011-09-30 39 views
1

我使用在github上(輝煌的東西)發現SBJson框架https://github.com/stig/json-framework/修改SBJson framwork我的應用程序

與例如:http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c

這嘰嘰喳喳比如現在的偉大工程。

所以我改變我的網址和

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"]); 
} 

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:@"message"] objectForKey:@"nationalad"]); 
} 

所以我在我的網頁JSON有消息:和nationalad:但我沒有得到任何回報或或登錄打印出。這些是我改變的唯一的兩件事。

任何想法?

這是編輯:

SBJsonParser *parser = [[SBJsonParser alloc] init]; 

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

// 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:@"message"] objectForKey:@"nationalad"]); 
    // NSLog(@"Message: %@", [status objectForKey:@"message"]); 

} 
    // NSDictionary *json = [NSString JSONValue]; 
    NSLog(@"Status: %@", statuses);  
    // NSArray *items = [statuses valueForKeyPath:@"data.array"]; 
    //NSLog(@"message : %@", [[items objectAtIndex:1] objectForKey:@"message"]); 

和服務器頁面:

{ 
'message': "<p style=\"color:#FFFFFF;font-family:'Century Gothic',futura,'URW Gothic L',Verdana,sans-serif;\">Welcome!<\/p><p style=\"color:#FFFFFF;font-family:'Century Gothic',futura,'URW Gothic L',Verdana,sans-serif;\">Check out today's Dinner and Lunch specials below!<\/p>", 
'nationalad': "<img src='http:\/\/www.mywebpage.com\/images\/national\/fullrz_3_4e81fa75ceba5_mywebpage.JPG'>" 
} 
+0

請編輯您的問題,併發布你試圖解析JSON字符串,以及你使用解析代碼它(從JSON字符串開始)。 – 2011-09-30 20:36:10

+0

NSLog(@「狀態:%@」,狀態);返回null – Rick

+0

在您發佈的代碼中作爲答案(您不應該這樣做,因爲它不是答案;您應該已經編輯了您的問題),如果status爲'nil',那麼您應該也有一個答案解析錯誤信息。什麼是錯誤信息? 'json_string'的內容是什麼? – 2011-10-02 23:31:43

回答

0

這不是有效的JSON - 所有字符串必須在雙引號內,包括名稱。如果您解決您的服務器,使其輸出

{ 
"message": "<p style=\"color:#FFFFFF;font-family:'Century Gothic',futura,'URW Gothic L',Verdana,sans-serif;\">Welcome!<\/p><p style=\"color:#FFFFFF;font-family:'Century Gothic',futura,'URW Gothic L',Verdana,sans-serif;\">Check out today's Dinner and Lunch specials below!<\/p>", 
"nationalad": "<img src='http:\/\/www.mywebpage.com\/images\/national\/fullrz_3_4e81fa75ceba5_mywebpage.JPG'>" 
} 

(注意,這兩個messagenationalad是雙引號內),SBJSON應該能夠解析您的JSON字符串。

但還有另一個問題:您的服務器沒有返回數組 - 它只是返回一個對象。無論哪種解決您的服務器代碼,以便它返回對象的數組,或者在客戶端代碼,解析一個對象:

NSDictionary *status = [parser objectWithString:json_string error:nil]; 

另外,還要注意使用nil

NSArray *statuses = [parser objectWithString:json_string error:nil]; 

你有效地告訴JSON解析器而不是在出現錯誤時返回錯誤對象。忽略錯誤通常是一個壞主意。你可以做這樣的事情:

NSError *jsonParseError; 
NSArray *statuses = [parser objectWithString:json_string error:&jsonParseError]; 
if (!statuses) { 
    // there's been a parse error; look at jsonParseError 
    // for example: 
    NSLog(@"JSON parse error: %@", jsonParseError); 
} 

或本:

NSError *jsonParseError; 
NSDictionary *status = [parser objectWithString:json_string error:&jsonParseError]; 
if (!status) { 
    // there's been a parse error; look at jsonParseError 
    // for example: 
    NSLog(@"JSON parse error: %@", jsonParseError); 
} 
0

首先的NSLog status,看看它是否是零。如果是,那麼你應該檢查你從中獲取JSON的URL。

如果URL爲零,則更正URL並重試。

相關問題