2011-09-30 44 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并重试。

相关问题