繁体   English   中英

解析json并获取异常,原因:'-[__ NSCFArray objectForKey:]:无法识别的选择器已发送到实例0x7b1c7630

[英]parsing json and getting exception, reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7b1c7630

我正在尝试从字典中获取特定键的值,但我得到了一个[__NSCFArray objectForKey:]: unrecognized selector sent to instance

- (void)listCaredMembersSuccessResponse:(NSDictionary *)response {
[self hideActivityView];

 if ([[response valueForKey:@"status"] caseInsensitiveCompare:NSLocalizedString(@"SUCCESS", nil)] == NSOrderedSame) {
   NSDictionary *mainDict = [response objectForKey:@"data"];
   NSArray *detailsArray = [mainDict objectForKey:@"Text"];
   [appDelegate.proxiesListArr addObjectsFromArray:[ParserManager parseListCaredMembers:detailsArray]];
 } else {
   [[ClassObjects sharedCenter] showCustomAlert:@"" Message:NSLocalizedString(@"PROXIES_FAILURERESPONSE", nil)];
  }

这是我的json响应:

{"Status":"Success","data":[{"Alias":"1-0","ID":80,"Icon":"","Items":[],"Params":{},"Text”:”Text1”,”Type":"group","Width":"170"},{"Alias":"1-1","ID":8000102,"Icon":"","Items":[],"Params":{},"Text”:”Text2”,”Type":"group","Width":"170"}]}

问题是您有一个NSArray而不是NSDictionary NSArray的计数为1,并包含NSDictionary

这行是错误的NSArray *detailsArray = [mainDict objectForKey:@"Text"];

NSArray *wrapper= [[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil]objectForKey:@"data"];

for (NSDictionary *temp in wrapper) {

   NSString *text=[temp objectForKey:@"Text"]; //Text may be NSString type


    // THE REST OF YOUR CODE
}

更新

if ([[response valueForKey:@"status"] caseInsensitiveCompare:NSLocalizedString(@"SUCCESS", nil)] == NSOrderedSame) {

NSArray *mainDict = [response objectForKey:@"data"];

  for (NSDictionary *temp in mainDict) {

   NSString *text=[temp objectForKey:@"Text"]; //Text may be NSString type


    // THE REST OF YOUR CODE
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM