2017-04-13 76 views
2

獲取數據我有一個小問題,下面的服務數據從服務目標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; 

} 
+0

可以遍歷'sdf.allKeys'來提取像正常陣列字典鍵值的數據,目前只能從'DataList1' – Tj3n

+0

獲得價值你的代碼是凌亂:如果'(([(的NSString * )sdf isEqual:[NSNull null]]))':爲什麼要投射?爲什麼使用雙括號? 'NSMutableDictionary * sdf = [(NSDictionary *)[responseString JSONValue] objectForKey:@「DataTable」];'我不知道'-JSONValue'是什麼,但您確定'sdf'是可變的,即'[( NSDictionary *)[responseString JSONValue] objectForKey:@「DataTable」]'是可變的嗎?等 – Larme

回答

1

你可以很容易地像

[myArray addObject:[sdf objectForKey:@"DataList1"]]; 

其開始與字典的數組,所以你需要你的第二個對象存儲陣列和指數走。

NSArray *temp = [sdf objectForKey:@"DataList2"]; 
    if(temp.count >0) 
    { 
    lblTeacherName.text = temp[0][@"TeacherName"]; 
    lblTeacherId.text = temp[0][@"TeacherId"]; 
    lblTeacherAge.text = temp[0][@"TeacherAge"]; 
    lblTeacherAddress.text = temp[0][@"TeacherAddress"]; 
    } 
+0

感謝它的工作 – Rajeev

2

請使用波紋管代碼,並通過您最初的Json解釋它。

-(void)parseJsonData:(NSDictionary *)jsonDictionary{ 

    for(int i=0;i<[jsonDictionary allKeys].count;i++){ 

     NSString *keyName = [[jsonDictionary allKeys] objectAtIndex:i]; 
     id objValue = [jsonDictionary objectForKey:keyName]; 

     if([objValue isKindOfClass:[NSArray class]]){ 

      NSArray *dataList2Array = (NSArray *)objValue; 
      NSLog(@"DataList2 Is :--%@",dataList2Array); 
     } 
     else { 

      NSDictionary *dataList1 = (NSDictionary *)objValue; 
      NSLog(@"DataList1 Is :--%@",dataList1); 
     } 
    } 
}