【问题标题】:Trying to parse a json crashes the app尝试解析 json 会使应用程序崩溃
【发布时间】:2016-04-27 10:47:30
【问题描述】:

我有以下 json

 {"status":1,"value":{"details":{"40404000024769":[{"name":"","email":""}]}}}

我解析如下,

NSString *statusCode = [NSString stringWithFormat:@"%@",jsonDic[@"status"]];
             NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
             NSArray *details = [valueDict objectForKey:@"details"];

             NSLog(@"%@",details);
             for (NSDictionary *response in details){
                 NSLog(@"adsf %@",response);
             }
         }

以下是我的错误日志

这可以得到唯一的40404000024769,但不能得到40404000024769的值。 我尝试使用[response valueForKey:@"name"] 并且应用程序崩溃了。 如何获取姓名、电子邮件的值?

2016-04-27 16:24:02.967 Vaighai Export[311:10625] ***Terminating app due to uncaught exception 'NSUnknownKeyException', reason:'[<__NSCFString 0x14e56870> valueForUndefinedKey:]:this class is not key value coding-compliant for the key name.'

【问题讨论】:

  • 你能显示崩溃报告吗
  • Details 有一个字典作为值。不是数组
  • @user31231234124 为什么您从我这里删除已接受的答案并将其交给 abhinandan,他基本上是从我的答案中复制代码,甚至懒得更改变量名?他是你的代理帐号吗?
  • 我希望你现在没有连续投票给我?这是不必要的。投票将在 24 小时内撤销,​​不会对我产生任何影响

标签: ios objective-c json parsing


【解决方案1】:

您的 JSON 结构是:

-NSDictionary
--Number
--NSDictionary
---NSDictionary
----NSDictionary
-----NSArray
------NSDictionary

details 有一个字典作为值,而不是您假设的数组。将您的代码更改为:

注意:这只是向您展示如何解析错误的示例。您需要在您的应用中处理真实世界 json 的案例。

NSString *statusCode = [NSString stringWithFormat:@"%i",jsonDic[@"status"]]; //We got status
NSDictionary *valueDict = [jsonDic objectForKey:@"value"]; //We got value dic
NSDictionary *detailDic = [valueDict objectForKey:@"details"]; //We got details dic

NSArray * internalArr = [detailDic objectForKey:@"40404000024769"]; //We got array of dictionaries
 //Iterate over this array to log internal dictionaries
for(NSDictionary *nameDic in internalArr)
{
    NSLog(@"Name: %@ email: %@",[nameDic objectForKey:@"name"],[nameDic objectForKey:@"email"]);
}

【讨论】:

    【解决方案2】:

    首先从详细信息字典中获取所有键,然后将该键放入数组中,然后在该数组的零索引处,该键可用,找到该键内的数组。

    【讨论】:

    • 可以参考我的代码详细说明一下吗?
    【解决方案3】:

    使用此代码

     NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
    
        NSDictionary *details = [valueDict objectForKey:@"details"];
    
        NSArray *YourArray= [details  objectForKey:@"40404000024769"];
    
        NSString *Name = [YourArray objectAtIndex:0]valueForKey:@"name"];
        NSString *Email = [YourArray objectAtIndex:0]valueForKey:@"email"];
    

    【讨论】:

      【解决方案4】:

      status 是一个整数

      NSInteger statusCode = [jsonDic[@"status"] integerValue];
      

      value 包含字典 @​​987654324@

      NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
      NSDictionary *details = [valueDict objectForKey:@"details"];
      

      details 包含字典中的数组。在数组的第 0 项中有一个字典,其中包含请求的 nameemail 键。

      for (NSString *key in details){
         NSLog(@"key %@",key);
         NSDictionary *data = details[key][0];
         NSLog(@"name %@ - email %@", data[@"name"], data[@"email"]);
      }
      

      【讨论】:

        【解决方案5】:

        key details 是字典而不是数组。

        只需添加一行即可获取40404000024769键值。

        如下修改你的代码:

        NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
        NSDictionary *details = [valueDict objectForKey:@"details"];
        NSArray *ar = [details objectForKey:@"40404000024769"];
        for (NSDictionary *response in ar){
            NSLog(@"name: %@ email: %@",[response objectForKey:@"name"],[response objectForKey:@"email"]);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-02-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多