解析两个JSON对象

3
当我发送API时,我会收到如下JSON响应。

{"ABC":[{"ID1":"response","ID2":"response","ID3":"response","ID4":"response","ID5":"response"},{"ID1":"response","ID2":"response","ID3":"response","ID4":"response","ID5":"response"},{"ID1":"response","ID2":"response","ID3":"response","ID4":"response","ID5":"response"}],"status":"OK","count":3}

{"XYZ":[{"id1":"response"},{"id1":"response"},{"id1":"response"}],"status":"OK","count":3}

这里的响应包括两个JSON对象。如何将这些数据存储在MutableArrays中。
我的代码是...
//Getting data from server through JSON approach ...
self.urlSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
self.urlReq= [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[[NSString alloc] initWithFormat:@"http://MyApiName"]]];

self.dataTask = [self.urlSession dataTaskWithRequest:self.urlReq completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (!(data == nil)) {
            
            self.loginDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"data : %@", data);
            NSLog(@"Login Dic : %@", [self.loginDic objectForKey:@"ABC"]);

            if (!(self.loginDic == nil)) {
                
                self.integer = [[self.loginDic objectForKey:@"ABC"] count];
                
                if ([[self.loginDic objectForKey:@"status"] isEqualToString:@"OK"] && (!(self.integer == 0))) {
                    
                    self.ID1 = [[NSMutableArray alloc]init];
                    self.ID2 = [[NSMutableArray alloc]init];
                    self.ID3 = [[NSMutableArray alloc]init];
                    self.ID4 = [[NSMutableArray alloc]init];
                    self.ID5 = [[NSMutableArray alloc]init];
                    
                    for (int i=0; i<self.integer; i++) {
                            
                        [self.ID1 addObject:[[[self.loginDic objectForKey:@"ABC"] objectAtIndex:i] objectForKey:@"ID1"]];
                        [self.ID2 addObject:[[[self.loginDic objectForKey:@"ABC"] objectAtIndex:i] objectForKey:@"ID2"]];
                        [self.ID3 addObject:[[[self.loginDic objectForKey:@"ABC"] objectAtIndex:i] objectForKey:@"ID3"]];
                        [self.ID4 addObject:[[[self.loginDic objectForKey:@"ABC"] objectAtIndex:i] objectForKey:@"ID4"]];
                        [self.ID5 addObject:[[[self.loginDic objectForKey:@"ABC"] objectAtIndex:i] objectForKey:@"ID5"]];                                
                    }
                    
                    NSLog(@"%@", self.ID1);
                    NSLog(@"%@", self.ID2);
                    
                } else {
                    
                }
                
            } else {
            
            }
            
        } else {
            
        }
        
    }];
    
    [self.dataTask resume];

}

我能获取数据,但是loginDic = null


你是从服务器那边这样发送的吗? - Juan
@Midhun MP,抱歉我编辑了上面的代码... - Naresh
1
@iOSDeveloper:你能否更新一下JSON?它仍然无效(你可以在这里验证你的JSON:http://jsoneditoronline.org)。但如果你从服务器得到的结果是这样的,那就是解析JSON时得到null的原因。 - Midhun MP
我遇到了错误。是的,谢谢... - Naresh
在序列化中添加错误引用,以检查为什么您没有得到任何内容的问题所在。 - Joshua
显示剩余4条评论
2个回答

1
在获得 AB 后,它会变成一个字典的数组。
你可以轻松地使用 valueForKeyPath,例如:
   NSArray * AB = @[@{
                      @"ID1":@"response",
                       @"ID2":@"response",
                      @"ID3":@"response",
                      @"ID4":@"response",
                      @"ID5":@"response",
                     },@{
                @"ID1":@"response",
                @"ID2":@"response",
                @"ID3":@"response",
                @"ID4":@"response",
                @"ID5":@"response"
                }];



    NSArray * ID1 = [AB valueForKeyPath:@"ID1"];
    NSArray * ID2 = [AB valueForKeyPath:@"ID2"];

0
如何同时接收两个JSON对象?
您展示的JSON确实是两个有效的JSON编码拼接而成。如果您的服务器正在执行此操作,并且您无法修复它,则可以尝试自行修复:
1. 任何出现的 `][`,`}{`,`]{` 或 `}[`(这两个字符之间带有任意数量的空格)都是一个JSON编码的结尾和下一个编码的开始。构造一个 `NSRegularExpression` 来查找这些序列并将其替换为相同的闭合/开放括号组合,但在它们之间添加逗号(`,`)。 2. 在开头添加单个 `[`,在结尾添加单个 `]`。
这两个步骤将会把您的拼接JSON编码转换为单独编码的JSON数组。现在按照通常的方式解析和处理,记得首先需要索引到最外层的数组才能访问特定的JSON响应,然后再索引到响应以访问您需要的元素。
希望对您有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接