1

I need to parse the following nested json.

{  
   "status":1,
   "value":{  
      "details":[  
         {  
            "text":"this is test",
            "company":"General Marketing Company",
            "date":"05-DEC-15"
         },
         {  
            "text":"this is test2",
            "company":"NJ, Chennai",
            "date":"05-DEC-15"
         },
         {  
            "text":"Sample test message for welcome",
            "company":"sd",
            "date":"22-JAN-16"
         }
      ]
   }
}

Following is my code,

NSString* testimonialResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSData *jsonData = [testimonialResponse dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];

NSLog(@"Response %@",jsonDic[@"value"][@"details"]);

The above code can actually be able to parse the contents within details in general but it cant be able to parse each of those datas uniquely.say for instance, my code doesn't list all company names,texts,dates uniquely.

2
  • try this if work then tell me so i can put it in answer self.Array=[jsonDic valueForKey:@"compnay"]; NSLog(@"Array : %@",self.Array); Commented Feb 18, 2016 at 10:18
  • What do you mean with "it can't be able to parse each of those datas"?. Your code parses the file correctly, the dates are strings so you shouldn't have any problem parsing it. Commented Feb 18, 2016 at 10:40

4 Answers 4

1
**You can use this**

 NSArray *array_details = [[jsonDic objectForKey:@"value"] objectForKey:@"details"];
 NSArray Compay_arr = [array_details valueForKey: @"company"];
 NSArray text_arr = [array_details valueForKey: @"text"];
 NSArray date_arr = [array_details valueForKey: @"date"];
Sign up to request clarification or add additional context in comments.

Comments

0

do like

NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
NSArray *dataArray = jsonDic[@"value"][@"details"];

 for (NSDictionary *tmp in dataArray)
{
 NSMutableDictionary *temp = [NSMutableDictionary new];
[temp setObject:[tmp objectForKey:@"text"] forKey:@"text"];
[temp setObject:[tmp objectForKey:@"company"] forKey:@"company"];
[temp setObject:[tmp objectForKey:@"date"] forKey:@"date"];

  [yourArrayName addObject:temp];

}

EDit

if you want to stop here , use this

NSArray * finalAray = [NSArray arrayWithArray: jsonDic[@"value"][@"details"]]

2 Comments

Why you should create another dictionary if each object in the array details is already a dictionary?
if he need use it else no need, in my assumption, if he modify it is easy
0

What about :

NSString* testimonialResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSData *jsonData = [testimonialResponse dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];

NSArray * array = [NSArray arrayWithArray: jsonDic[@"value"][@"details"]]

Comments

0
NSArray *array = [NSArray arrayWithArray: jsonDic[@"value"][@"details"]];
NSArray *companyNames = [array valueForKey:@"company"];

companyNames contains all company names

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.