當我發送API時,我得到JSON這樣的迴應。如何一次接收兩個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}
在這裏,我收到了這樣的回覆。如何將這些數據存儲在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
。
首先,您發佈的JSON不是有效的。第二件事是你的代碼沒有任何意義,因爲你的JSON中的鍵和代碼中的鍵是不同的。 –
你是從服務器那邊發送的嗎? – Juan
@Midhun MP對不起,我編輯了上面的代碼... – iOS