我正在使用在 github 上找到的 SBJson 框架(很棒的东西)https://github.com/stig/json-framework/
例如:http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c
这个 Twitter 示例现在运行良好。
所以我改变了我的 url 和
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 有 message: 和 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'>"
}
最佳答案
这不是有效的 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'>"
}
(注意 message
和 nationalad
都在双引号内),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);
}
关于objective-c - 为我的应用修改 SBJson 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7614580/