【问题标题】:iOS 5 Google JSON - How to parse nested object?iOS 5 Google JSON - 如何解析嵌套对象?
【发布时间】:2012-04-24 12:36:56
【问题描述】:

我正在尝试解析 Youtube 播放列表:

如果我的 JSON 结构如下:

{"apiVersion" .... 
"items":[{"id2":"some-id","title":"songtitle",

我完全可以通过以下方式解析标题:

// Fill array
NSArray *items = [json objectForKey:@"items"];

// Get item from tableData
NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
// Set text on textLabel
[[cell textLabel] setText:[item objectForKey:@"title"]];

但是如果 JSON 是这样的:

{"apiVersion" .... 
"items":[{"id1": .... "video":{"id2":"some-id","title":"songtitle",

如何获得嵌套对象的标题?

只是一件简单的事情,但我已经为此苦苦思考了好几个小时。郁闷了,谢谢你的建议!

[编辑] 这是完整的结构:

{
    "items":
    [
    {
    "video":
            {
                "title": "Number One",
                "description": "Description one"
            },    
            {
             "title": "Number two",
                "description": "Description two"
            },
            {
                "title": "Number three",
                "description": "Description three"
            }
    },
    {   
    "video":
            {
         "title": "Number One",
             "description": "Description one"
            },
            {
                "title": "Number two",
              "description": "Description two"
            },
            {
               "title": "Number three",
              "description": "Description three"
            }
    }

    ]
}

【问题讨论】:

  • 你能展示你的整个 JSON 吗,这样有点混乱......

标签: iphone ios json ios5


【解决方案1】:

试试这个:

NSArray *items = [[json valueForkey:@"items"]valueForkey:"video"];

【讨论】:

  • 不,也没有效果。还必须在项目和大写 valueForKey 之前添加 @。
【解决方案2】:
NSMutableArray *items = [json valueForkey:@"items"];
for(int i =0;i<[items count]; i++)
{
  NSMutableArray *arrtitle = [[[items objectAtIndex:i]valueForkey:@"Video"]copy];
  for(int j =0;j<[arrtitle count]; j++)
  {
    NSString *title = [[arrtitle objectAtIndex:j]valueForkey:@"title"];
  }
} 

也许对你有帮助。

【讨论】:

  • 不,无济于事,适用于第一段 json,但不适用于第二段。 Xcode 还报告 title 是一个未使用的变量。 NSLog 回显 null。
  • 原因可能在 [ ]。我推测这个NSMutableArray *items = [json valueForkey:@"items"]; 和这个NSMutableArray *arrtitle = [[[items objectAtIndex:i]valueForkey:@"Video"]copy]; 没有做同样的事情。由于第二部分不包含 [ ] 。简单地说,它不是嵌套的 JSON。您可能需要一个真正的解析器。只是我的 2 美分。
  • json 层次结构是数组> 字典> 字典。所以我们必须计算字典关键视频。所以我们得到了更多的三个字典。
【解决方案3】:

问题是您的 json 无效。所有titles 都应该在数组中,对吗?所以他们必须在[]中。例如,您可以使用以下站点来“调试”您的 json:

http://jsonformatter.curiousconcept.com/

此外,在 iOS 5 中,您可以使用新的内置 JSON api。这是一个很好的教程: http://www.raywenderlich.com/5492/working-with-json-in-ios-5

不过,我猜你的 json 应该是这样的:

{
   "items":[
      {
         "video":[
            {
               "title":"Number One",
               "description":"Description one"
            },
            {
               "title":"Number two",
               "description":"Description two"
            },
            {
               "title":"Number three",
               "description":"Description three"
            }
         ]
      },
      {
         "video":[
            {
               "title":"Number One",
               "description":"Description one"
            },
            {
               "title":"Number two",
               "description":"Description two"
            },
            {
               "title":"Number three",
               "description":"Description three"
            }
         ]
      }
   ]
}

祝你的项目好运;)

【讨论】:

  • 谢谢!我也没发明它
  • @Martindem Ok :), ... 但 YouTube 提供的 JSON 仍然是有效的 json。它们将项目作为数组,并且数组中的每个对象都是一个字典。您的示例具有不同的含义并且无效(就 json 而言)。无论如何,我希望这篇文章对你有所帮助。再一次,祝你好运;)
  • 是的,只是注意到我的简化无效低头 /直接回到代码中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-01
  • 2020-05-18
  • 2019-12-18
相关资源
最近更新 更多