ios - 解析 json 并出现异常,原因 : '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7b1c7630

标签 ios objective-c iphone xcode

我正在尝试从字典中获取特定键的值,但我得到了一个[__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 而不是 NSDictionaryNSArray 的计数为 1,并包含一个 NSDictionary

this line is wrong 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
}

}

关于ios - 解析 json 并出现异常,原因 : '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7b1c7630,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35238484/

相关文章:

objective-c - 在屏幕之间保存数据

ios - CALayerInvalidGeometry 在 iOS9 上崩溃 "sublayer with non-finite position [inf inf]"

iphone - Xcode 5 自动布局 : collection of views inside another view

android - 如何更改 Facebook 登录身份验证屏幕中的默认应用程序图标

ios - 如何在 ios swift 中使用 Storyboard平均划分按钮图像?

ios - 更改swift4中while循环的声音文件

iphone - 更改 MKMapView 中当前位置的图像

ios - 如何在 uitableview 上制作固定高度的单元格?

objective-c - 在 dealloc 中使用 self.property = nil 有什么问题吗?

objective-c - 如何为 ios 中的方法定义默认参数值?