Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 试图解析json会使应用程序崩溃_Ios_Objective C_Json_Parsing - Fatal编程技术网

Ios 试图解析json会使应用程序崩溃

Ios 试图解析json会使应用程序崩溃,ios,objective-c,json,parsing,Ios,Objective C,Json,Parsing,我得到了以下json {"status":1,"value":{"details":{"40404000024769":[{"name":"","email":""}]}}} 我把它解析如下 NSString *statusCode = [NSString stringWithFormat:@"%@",jsonDic[@"status"]]; NSDictionary *valueDict = [jsonDic objectForKey:@"value"];

我得到了以下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);
             }
         }
下面是我的错误日志

这只能获取
4040400024769
,但不能获取
4040400024769
的值。 我尝试使用
[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.'
2016-04-27 16:24:02.967 Vaighai Export[311:10625]***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[valueForUndefinedKey:]:此类不符合密钥名称的键值编码。”

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

您的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"]);
}
使用此代码

 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"];

状态
是一个整数

NSInteger statusCode = [jsonDic[@"status"] integerValue];
value
包含一个字典
details

NSDictionary *valueDict = [jsonDic objectForKey:@"value"];
NSDictionary *details = [valueDict objectForKey:@"details"];
详细信息
包含字典中的数组。在数组的项0中有一个字典,其中包含请求的
名称
电子邮件

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

key
details
是字典而不是数组

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

更改代码如下:

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"]);
}

可以显示崩溃报告详细信息是否有一个字典作为值。不是array@user31231234124你们为什么从我这里删除接受的答案,并把它交给阿比南丹,他基本上是从我的答案中复制代码的,甚至懒得修改变量名?他是你的代理帐户吗?我希望你现在没有投票给我?没必要。投票将在24小时内被撤销,不会对mecan产生任何影响。请参考我的代码详细说明?