【问题标题】:SBJson won't go as deep in NSDictionarySBJson 不会深入 NSDictionary
【发布时间】:2012-08-31 01:29:37
【问题描述】:

因为这个,我整天都在用头撞墙。

我正在尝试解析这个 JSON blob here

这是我正在使用的:

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSDictionary *results = [responseString JSONValue];

NSArray *allTweets = [[results objectForKey:@"response"] objectForKey:@"posts"];

但是,当我尝试这样做时:

NSURL *url = [NSURL URLWithString:[[[[aTweet objectForKey:@"posts"] objectForKey:@"photos"] objectForKey:@"original_size"] objectForKey:@"url"]];

它没有给我任何错误,但 *url 设置为“null”。

我在 NSDictionary 中使用了 CFShow,但 "photos" 键之后的所有内容都是常规字符串,而不是 JSON 格式。

谁能告诉我为什么?

提前感谢您的帮助。

【问题讨论】:

  • 尽管“单行”看起来不错,但在处理 JSON 时,将数据结构遍历中的每一步分解为单独的行几乎总是更明智的做法。当你错过了一个步骤时更容易看到,并且更容易调试。效率上的差异(如果确实有的话)可以忽略不计。

标签: iphone xcode json nsdictionary sbjson


【解决方案1】:

试试这个。

    for (int i = 0; i < [[[results objectForKey:@"response"] objectForKey:@"posts"] count]; i++) {
        NSLog(@"url data = %@",[[[[[[[results objectForKey:@"response"]
        objectForKey:@"posts"] objectAtIndex:i] objectForKey:@"photos"]
        objectAtIndex:0] objectForKey:@"original_size"] objectForKey:@"url"]);
    }

【讨论】:

  • 是的,就是这样!我错过了 objectAtIndex:0 谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多