gpt4 book ai didi

ios - 解析两个 JSON 对象

转载 作者:搜寻专家 更新时间:2023-10-30 20:24:00 24 4
gpt4 key购买 nike

当我发送 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

最佳答案

在你得到 AB 之后,它的 Dictionary 数组

您可以轻松地使用此 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"];

关于ios - 解析两个 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45931116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com