0

我正在尝试解析 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"
            }
    }

    ]
}
4

3 回答 3

3

试试这个:

NSArray *items = [[json valueForkey:@"items"]valueForkey:"video"];
于 2012-04-24T13:04:04.530 回答
1
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"];
  }
} 

也许它会帮助你。

于 2012-04-24T12:43:50.703 回答
0

问题是您的 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"
            }
         ]
      }
   ]
}

祝你的项目好运;)

于 2012-04-25T06:06:21.483 回答