我可以调用API并检索JSON数据。试图从JSON数据中获取值内的值
下面的代码返回字典对象:
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:ResponseData options:kNilOptions error:&error];
NSDictionary *hits = dataDictionary[@"hits"];
NSLog(@"%@", hits);
“点击”返回此数据:
{
"_id" = 521b95cf4a56d006d578c213;
"_index" = "f762ef22-e660-434f-9071-a10ea6691c27";
"_score" = "3.6973438";
"_type" = item;
fields = {
"brand_id" = 513fbc1283aa2dc80c000005;
"brand_name" = Subway;
"item_id" = 521b95cf4a56d006d578c213;
"item_name" = "6\" Subway Melt\U00ae";
"nf_serving_size_qty" = 1;
"nf_serving_size_unit" = serving;
};
我试图获得“ITEM_NAME”的价值,所以我尝试它像这样:
NSString *nameOfItem = hits[@"item_name"];
NSLog(@"%@", nameOfItem);
但是,我导致SIGABRT错误,并显示以下消息:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7f97c0d85520'
我该如何去取回“item_name”值?
抱歉,但这看起来不是有效的JSON。无论如何,我看到“字段”下的“item_name” –
错误消息告诉你,你有一个数组,但你显示的命中是一个字典。这是从您的日志完整打印输出?尝试记录[点击课程],看看有什么。 – rdelmar
“点击”没有返回上面的数据。在上面的清单中省略了数据周围的'()'字符。学习如何阅读JSON! –