Rajeev Rajeev - 7 months ago 26
iOS Question

Getting data from a services Objective c

I have got a small problem with the below service data

{
"DataTable": {
"DataList1": {

"StudentName": "Rakesh",
"StudentId": "13",
"StudentAge": "19",
"StudentAddress": "NewYork",
},
"DataList2": [{
"TeacherName": "Abhinav",
"TeacherId": "309",
"TeacherAge": "34",
"TeacherAddress": "NewYork",
}]
}
}


i Can get the data from DataList1 and cannot know how to get the data from DataList2. Below is the code what i have tried. Please help to find out the solution. Thanks in Advance

- (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;

}

Answer Source

you can get easily like

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

its started with array of dictionary , so you need to store your second object to array and take from index.

  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"];
  }