简体   繁体   中英

JSON parsing error in Objective-C

I am trying to parse this JSON in Objective-C. The response object looks thus:

(
  {
    "Year": "2003",
    "SumOfYear": "0.20"
  },
  {
    "Year": "2004",
    "SumOfYear": "0.64"
  },
  {
    "Year": "2005",
    "SumOfYear": "0.90"
  }
)

I tried the following

NSDictionary* dictionaryObtained = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];

NSLog(@"dict = %@",dictionaryObtained);
NSDictionary *yearsObtained = [dictionaryObtained objectForKey:@"Year"];

But I obtain the following error:

-[__NSCFArray bytes]: unrecognized selector sent to instance 0x18a3bfd0

Where am I going wrong? I want to obtain all the Year in a NSArray and all the SumOfYear in another NSArray .

The error is from this line

NSDictionary* dictionaryObtained = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];

It appears (hard to tell for sure without better info from you) that responseObject has already been parsed from JSON string into Objective-C objects. Therefore you should not run it through NSJSONSerialization again.

But what you have is an NSArray, so, assuming you want to collect an array of the "Year" values, you need something along the lines of:

NSMutableArray* yearsObtained = [NSMutableArray array];
for (NSDictionary* dictionaryObtained in responseObject) {
    NSLog(@"dict = %@",dictionaryObtained);
    NSString* year = [dictionaryObtained objectForKey:@"Year"];
    NSLog(@"year = %@", year);
    [yearsObtained addObject:year];
}

I would suggest to do it like that:

NSArray * dataArray =  [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
for(NSDictionary * diction in dataArray)
{ 
    NSLog(@"%@",[diction objectForKey:@"Year"]);
}

because you have an array of dictionaries you should put a JSONSerialization inside an NSArray then to go inside it with dictionary thus u can get to your years as you wish.

From comments below it seems like your responseObject is already parsed so you can just go

 for(NSDictionary* dict in responseObject)
 {
     NSLog(@"%@",[dict objectForKey:@"Year"]);
 }

You have wrong(right format - but not to match your code) the json. If you want your code to work this is the correct:

{ "Year":
    [
          {
          "Year": "2003",
          "SumOfYear": "0.20"
          },
          {
          "Year": "2004",
          "SumOfYear": "0.64"
          },
          {
          "Year": "2005",
          "SumOfYear": "0.90"
          }
     ]
}

Your json as it is can be parsed:

for(NSDictionary *myDict in jsonObj){
     NSString *year = [myDict objectForKey:@"Year"];
}

The simplest way to parse your JSON and get two arrays is

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];

NSArray *years = [jsonArray valueForKeyPath:@"Year"];
NSArray *sums = [jsonArray valueForKeyPath:@"SumOfYear"];

KVC is a great instrument for parsing data structures.

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