【问题标题】:Modifying the SBJson framwork for my app为我的应用修改 SBJson 框架
【发布时间】: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);返回空
  • 在你作为答案发布的代码中(你不应该有,因为它不是答案;你应该编辑你的问题),如果 statusnil 那么你应该也有一个解析错误消息。错误信息是什么?而json_string的内容是什么?
  • @Bavarious。非常感谢帮忙。对象而不是数组。我也在推特 URL 上试用了它,它工作正常。出于某种原因(仍在检查),我不得不在 JSON 上加上双引号。 ""消息"" : ""xxxxx""。但它现在正在工作。剩下的就是让它加载一个带有结果的 web 视图。

标签: objective-c ios json


【解决方案1】:

这不是有效的 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 in

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

【讨论】:

    【解决方案2】:

    首先,NSLog status 看看是否为nil。如果是,那么您应该检查您从中获取 JSON 的 URL。

    如果 URL 为 nil,则更正 URL,然后重试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      相关资源
      最近更新 更多