【发布时间】:2011-09-30 18:32:25
【问题描述】:
我正在使用在 github 上找到的 SBJson 框架(很棒的东西)https://github.com/stig/json-framework/
例如:http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c
这个 twitter 示例现在效果很好。
所以我改变了我的网址和
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'>"
}
【问题讨论】:
-
请编辑您的问题并发布您尝试解析的 JSON 字符串,以及您用于解析它的代码(从 JSON 字符串开始)。
-
NSLog(@"状态:%@", statuses);返回空
-
在你作为答案发布的代码中(你不应该有,因为它不是答案;你应该编辑你的问题),如果
status是nil那么你应该也有一个解析错误消息。错误信息是什么?而json_string的内容是什么? -
@Bavarious。非常感谢帮忙。对象而不是数组。我也在推特 URL 上试用了它,它工作正常。出于某种原因(仍在检查),我不得不在 JSON 上加上双引号。 ""消息"" : ""xxxxx""。但它现在正在工作。剩下的就是让它加载一个带有结果的 web 视图。
标签: objective-c ios json