獲取數據我有一個小問題,下面的服務數據從服務目標C
{
"DataTable": {
"DataList1": {
"StudentName": "Rakesh",
"StudentId": "13",
"StudentAge": "19",
"StudentAddress": "NewYork",
},
"DataList2": [{
"TeacherName": "Abhinav",
"TeacherId": "309",
"TeacherAge": "34",
"TeacherAddress": "NewYork",
}]
}
}
我可以從DataList1獲取數據並不能知道如何從DataList2獲取數據。下面是我嘗試過的代碼。請幫助找出解決方案。由於提前
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[jsonArray removeAllObjects];
NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
NSMutableDictionary *sdf = [(NSDictionary*)[responseString JSONValue] objectForKey:@"DataTable"];
NSMutableArray * myArray = [[NSMutableArray alloc] init];
if (([(NSString*)sdf isEqual: [NSNull null]])) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Information" message:@"Currently there are no Data" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert setTag:1];
[alert show];
}else {
[myArray addObject:[sdf objectForKey:@"DataList1"]];
jsonArray=[myArray mutableCopy];
refilldict=[jsonArray objectAtIndex:0];
NSArray *keys = [refilldict allKeys];
for(int p=0 ; p<[keys count]; p++)
{
NSString *value=[refilldict objectForKey:[keys objectAtIndex:p]];
if ([value isEqual: [NSNull null]]||[value length]==0) {
[refilldict setValue:@"" forKey:[keys objectAtIndex:p]];
}
}
lblStudentName.text = [refilldict objectForKey:@"StudentName"];
lblStudentId.text = [refilldict objectForKey:@"StudentId"];
lblStudentAge.text = [refilldict objectForKey:@"StudentAge"];
lblStudentAddress.text = [refilldict objectForKey:@"StudentAddress"];
}
self.navigationController.navigationBar.userInteractionEnabled = YES;
[HUD hide:YES];
[HUD removeFromSuperview];
HUD=nil;
}
可以遍歷'sdf.allKeys'來提取像正常陣列字典鍵值的數據,目前只能從'DataList1' – Tj3n
獲得價值你的代碼是凌亂:如果'(([(的NSString * )sdf isEqual:[NSNull null]]))':爲什麼要投射?爲什麼使用雙括號? 'NSMutableDictionary * sdf = [(NSDictionary *)[responseString JSONValue] objectForKey:@「DataTable」];'我不知道'-JSONValue'是什麼,但您確定'sdf'是可變的,即'[( NSDictionary *)[responseString JSONValue] objectForKey:@「DataTable」]'是可變的嗎?等 – Larme