【问题标题】:JSON parsing error in Objective-CObjective-C 中的 JSON 解析错误
【发布时间】:2014-03-16 22:10:48
【问题描述】:

我正在尝试在 Objective-C 中解析这个 JSON。响应对象如下所示:

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

我尝试了以下

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

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

但我收到以下错误:

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

我哪里出错了? 我想获得NSArray 中的所有Year 和另一个NSArray 中的所有SumOfYear

错误来自这一行

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

【问题讨论】:

  • 为什么年份使用转义字符? (在最后一行)。
  • 你的字典打印出来是什么样子的?
  • xcode 不解析 JSON。它只是一个 IDE。
  • @vikingosegundo 怎么了? “我正在尝试在 xcode 中解析这个 JSON”他试图在名为 xcode 的 IDE 中解析 JSON,他可以在 Visual Studio 或 Eclipse 上尝试,但他在 xcode 中尝试......问题标记为 Objective-C 而不是 xcode
  • 如果您实际查看 JSON,您有一个包含“对象”(字典)的数组。访问 json.org 并花 5 到 10 分钟来学习 JSON 语法——这是对您时间的非常明智的投资。

标签: ios objective-c json cocoa-touch parsing


【解决方案1】:

看来(如果没有您提供更好的信息,很难确定)responseObject 已经从 JSON 字符串解析为 Objective-C 对象。因此,您不应该再次通过 NSJSONSerialization 运行它。

但是您拥有的是一个 NSArray,因此,假设您要收集“年份”值的数组,您需要以下内容:

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

【讨论】:

    【解决方案2】:

    你有错误(正确的格式 - 但不匹配你的代码)json。 如果您希望您的代码正常工作,这是正确的:

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

    您的 json 可以被解析:

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

    【讨论】:

      【解决方案3】:

      我建议这样做:

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

      因为你有一个字典数组,你应该将 JSONSerialization 放在 NSArray 中 然后用字典进入它,这样你就可以随心所欲地度过你的岁月了。

      从下面的 cmets 看来,您的 responseObject 已被解析 所以你可以去

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

      【讨论】:

      • responseObject 是什么?是 NSData 吗?
      • 这是我使用的`[manager POST:postRequestString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)`
      • 错误在哪一行触发?(尝试调试它)
      • 他的问题是“responseObject”已经被解析过了,是一个NSArray。
      • @HotLicks 用那个有用的注释更新了答案,希望它现在可以工作)
      【解决方案4】:

      解析 JSON 并获取两个数组的最简单方法是

      NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
      
      NSArray *years = [jsonArray valueForKeyPath:@"Year"];
      NSArray *sums = [jsonArray valueForKeyPath:@"SumOfYear"];
      

      KVC 是解析数据结构的绝佳工具。

      【讨论】:

      • 我试过这个但得到了同样的错误。你是说responseObject 代表jsonArray
      • 我刚刚用你的阵列试了一下。没关系!顺便说一句,如果你的 jsonArray 没问题,它和 for (NSDictionary *myDict in jsonArray) 完全一样。
      • 不,我的意思是我们从 [NSJSONSerialization JSONObjectWithData: ...] 得到的数组
      猜你喜欢
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多