How Can i Parse the Following JSON data using NSJSONSerialization in iOS

Rv rvikar
Rv rvikar used Ask the Experts™
on
{
  "EmployeeApp": [
    {
      "id": "1",
      "name": "Mr.ABC",
      "email": "ABC@XYZ.COM",
      "address": "xxx",
      "hr_review": {
        "1": {
          "hr_name": "Mr. f r",
          "hr_id": "13",
          "hr_comment": "he is one of the nice employee for company",
        },
        "2": {
          "hr_name": "Mr. b c",
          "hr_id": "14",
          "hr_comment": "Takes pride in work and strives to improve work performance",
        }
      }
    },
    {
      "id": "2",
      "name": "Mr.PQR",
      "email": "PQR@XYZ.COM",
      "address": "xxx",
    "hr_review": {
        "1": {
          "hr_name": "Mr. f r",
          "hr_id": "13",
          "hr_comment": "Forms and required paperwork are completed on time with minimal errors.",
        },
        "2": {
          "hr_name": "Mr. b c",
          "hr_id": "14",
          "hr_comment": "Maintains good working relationships with coworkers",
        }
      }
    }
  ]
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
Some code doing the job :
    NSData *jsonString = [@"{\"EmployeeApp\":[{\"id\":\"1\",\"name\":\"Mr.ABC\",\"email\":\"ABC@XYZ.COM\",\"address\":\"xxx\",\"hr_review\":{\"1\":{\"hr_name\":\"Mr. f r\",\"hr_id\":\"13\",\"hr_comment\":\"he is one of the nice employee for company\"},\"2\":{\"hr_name\":\"Mr. b c\",\"hr_id\":\"14\",\"hr_comment\":\"Takes pride in work and strives to improve work performance\"}}},{\"id\":\"2\",\"name\":\"Mr.PQR\",\"email\":\"PQR@XYZ.COM\",\"address\":\"xxx\",\"hr_review\":{\"1\":{\"hr_name\":\"Mr. f r\",\"hr_id\":\"13\",\"hr_comment\":\"Forms and required paperwork are completed on time with minimal errors.\"},\"2\":{\"hr_name\":\"Mr. b c\",\"hr_id\":\"14\",\"hr_comment\":\"Maintains good working relationships with coworkers\"}}}]}"
                    dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError *error = nil;

    NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:jsonString options:0 error:&error];
    NSArray *EmployeeApp = [jsonData valueForKey:@"EmployeeApp"];

    // THIS IS FOR THE FIRST EMPLOYEE
    NSDictionary *employee = EmployeeApp[0];
    NSString *employeeId = [employee valueForKey:@"id"];
    NSString *employeeName = [employee valueForKey:@"name"];
    NSLog(@"%@", [employeeId stringByAppendingString:[@"," stringByAppendingString:employeeName]]);
  
    for(unsigned int i=0;i<EmployeeApp.count;i++)
    {
        NSDictionary *_employee = EmployeeApp[i];
        NSString *_employeeId = [_employee valueForKey:@"id"];
        NSString *_employeeName = [_employee valueForKey:@"name"];
        NSDictionary *reviews = [_employee valueForKey:@"hr_review"];
        NSString *comments = @"";
        for(id key in reviews)
        {
            NSString *hr_name = [[reviews valueForKey:key] valueForKey:@"hr_name"];
            NSString *hr_id = [[reviews valueForKey:key] valueForKey:@"hr_id"];
            NSString *hr_comment = [[reviews valueForKey:key] valueForKey:@"hr_comment"];
            comments = [comments stringByAppendingString:[@"\n" stringByAppendingString:hr_comment]];
        }
        NSLog(@"%@", [_employeeId stringByAppendingString:[@"," stringByAppendingString:[_employeeName stringByAppendingString:[@" got following the comments: " stringByAppendingString:comments]]]]);
    }

Open in new window


output :
2016-11-26 07:49:51.098484 test[4211:554323] 1,Mr.ABC
2016-11-26 07:50:03.749720 test[4211:554323] 1,Mr.ABC got following the comments: 
he is one of the nice employee for company
Takes pride in work and strives to improve work performance
2016-11-26 07:50:09.544194 test[4211:554323] 2,Mr.PQR got following the comments: 
Forms and required paperwork are completed on time with minimal errors.
Maintains good working relationships with coworkers

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial