简体   繁体   中英

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

I am trying to get a value for a particular key from a dictionary but i get a [__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)];
  }

This is my json response:

{"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"}]}

The problem is you have a NSArray not an NSDictionary . The NSArray has a count of 1 and contains an 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
}

Update

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
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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