Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.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 使用动态响应objective-c解析Json_Ios_Iphone_Json - Fatal编程技术网

Ios 使用动态响应objective-c解析Json

Ios 使用动态响应objective-c解析Json,ios,iphone,json,Ios,Iphone,Json,我在分析以下错误响应以在UIAlertView中显示时遇到问题- {"error": { "email": { "isEmpty": "Value is required and can't be empty" }, "password": { "isEmpty": "Value is required and can't be empty" }, "name": { "isEmpty": "Value is required and can't be empty" }, "surn

我在分析以下错误响应以在UIAlertView中显示时遇到问题-

{"error": {
"email": {
  "isEmpty": "Value is required and can't be empty"
},
"password": {
  "isEmpty": "Value is required and can't be empty"
},
"name": {
  "isEmpty": "Value is required and can't be empty"
},
"surname": {
  "isEmpty": "Value is required and can't be empty"
}
},
通常我会这样做来获取每个字典中的对象,但问题是键会根据错误而变化。我如何正确地解析这个

NSDictionary * Json = [NSJSONSerialization JSONObjectWithData:data
                                                                  options:kNilOptions
                                                                  error:&error];
NSDictionary * error = [Json objectForKey:@"error"];
for (NSDictionary * subError in error) {
                 NSLog(@"subError = %@", subError);
             }
这将只打印电子邮件、密码、姓名和姓氏。我可以这样做,但我并不总是知道第二把钥匙是什么。在这种情况下,它是“密码”-


你不应该重复使用词典。您应该尝试以下方法:

NSDictionary * error = [Json objectForKey:@"error"];
for (NSString * subErrorKey in [error allKeys]) {
    NSLog(@"subError = %@", [[error objectForKey:subErrorKey] objectForKey:isEmpty]);
}

使用以下代码进行操作-

NSDictionary * subError = [[luckyNumbers objectForKey:@"error"] objectForKey:[[error allKeys] objectAtIndex:0]];
title = [[error allKeys] objectAtIndex:0];
message = [[[luckyNumbers objectForKey:@"error"] objectForKey:[[error allKeys] objectAtIndex:0]] objectForKey:[[subError allKeys] objectAtIndex:0]];

UIAlertView *alert = [[UIAlertView alloc]
                                           initWithTitle:title
                                           message:message
                                           delegate:nil
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles:nil];
                     [alert show];

第一个问题是缺少JSON列表中的前导
{
。第二个问题是没有阅读NSDictionary的规范。添加了{并且不确定在阅读NSDictionary的规范时你指的是什么,这意味着如果你阅读了规范,你就不需要问这个问题。这没有任何意义。你在重复字典,尽管你说了“不”,但你使用了未定义的值
isEmpty
,作为一个键。这与我所做的非常相似问题是我可能不知道“isEmpty”键。这可能是另一个键。我正在尝试看看是否有一种方法可以做到这一点,而不必事先知道每个键。
NSDictionary * subError = [[luckyNumbers objectForKey:@"error"] objectForKey:[[error allKeys] objectAtIndex:0]];
title = [[error allKeys] objectAtIndex:0];
message = [[[luckyNumbers objectForKey:@"error"] objectForKey:[[error allKeys] objectAtIndex:0]] objectForKey:[[subError allKeys] objectAtIndex:0]];

UIAlertView *alert = [[UIAlertView alloc]
                                           initWithTitle:title
                                           message:message
                                           delegate:nil
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles:nil];
                     [alert show];