简体   繁体   English

Objective-C中的JSON解析错误

[英]JSON parsing error in Objective-C

I am trying to parse this JSON in Objective-C. 我正在尝试在Objective-C中解析此JSON。 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 . 我想在一个NSArray获取所有Year ,在另一个NSArray获取所有SumOfYear

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. 似乎(如果没有您提供更好的信息,很难确定) responseObject已经从JSON字符串解析为Objective-C对象。 Therefore you should not run it through NSJSONSerialization again. 因此,您不应再次通过NSJSONSerialization运行它。

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: 但是您拥有的是一个NSArray,因此,假设您要收集“ Year”值的数组,则需要遵循以下步骤:

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. 因为您有一个字典数组,所以应该将JSONSerialization放入NSArray中,然后将其与字典一起放入其中,这样您就可以按自己的意愿去岁。

From comments below it seems like your responseObject is already parsed so you can just go 从下面的评论中,您的responseObject似乎已经被解析,因此您可以

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

You have wrong(right format - but not to match your code) the json. 您输入了错误的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: 可以解析您的json:

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

The simplest way to parse your JSON and get two arrays is 解析JSON并获取两个数组的最简单方法是

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. KVC是解析数据结构的绝佳工具。

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

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