目前我正在开发一个应用程序,该应用程序将有一个用于新闻部分的 UITableView。我将进行 API 调用以检索新闻以填充 tableView 中的单元格。目前我正在通过 JSON 返回数据,这是 JSON 响应的样子:(放在换行符上,这样阅读结构会更容易一些)
[
{
"news_id": "1",
"news_title": "Sample headline",
"news_date": "2012-12-19",
"news_news": "news article 1",
"news_priority": "3"
},
{
"news_id": "2",
"news_title": "On the Outside, Looking in",
"news_date": "2012-11-20",
"news_news": "news article 2",
"news_priority": "2"
},
{
"news_id": "3",
"news_title": "Fla. Plans to Mark Death Penalty's Return",
"news_date": "2012-12-23",
"news_news": "news article 3",
"news_priority": "1"
}
]
在通过 php 进行 JSON 编码之前,我的数组如下所示:
Array
(
[0] => Array
(
[news_id] => 1
[news_title] => Sample headline
[news_date] => 2012-12-19
[news_news] => news article 1
[news_priority] => 3
)
[1] => Array
(
[news_id] => 2
[news_title] => On the Outside, Looking in
[news_date] => 2012-11-20
[news_news] => news article 2
[news_priority] => 2
)
[2] => Array
(
[news_id] => 3
[news_title] => Fla. Plans to Mark Death Penalty's Return
[news_date] => 2012-12-23
[news_news] => news article 3
[news_priority] => 1
)
)
我通过执行以下操作来存储响应:
// populate the dictionary with the json response.
NSDictionary *data = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:&error];
return data;
// model has a method to construct the url request/connection etc
NSDictionary *newsDict = [model makeServerAPICall:kURL postMessage:@"method=getNews"];
NSString *string = [newsDict objectForKey:@"news_id"];
到目前为止,一切似乎都很好,除非我尝试访问数组内部的元素时出现此崩溃并出现错误:
-[__NSArrayM objectForKey:]:无法识别的选择器发送到实例 0xab13a40 即使这是一个 NSDictionary。在这一点上我很困惑,因为“数据”是一个 NSDictionary。
我正在尝试使用将news_title列出的返回的数据设置我的表观视图,并在选定时将其推向详细视图,以显示该文章的相对数据。我进行了许多 API 调用,其中返回的数据是非常简单的 JSON 格式。这是我第一次尝试从 JSON 响应中提取包含多个数组的数据。老实说,在这一点上,我不知道从这里还能去哪里。任何帮助将非常感激。