0

How to extract JSON data dictionary. I trying to many times but cant extract particular value. I get only first "business_category_name" but "business_details" under value "name" cant fetch. how it possible please help.

Thank you

My JSON Data

{
 "status": [
{
  "business_category_name": "Banks\/Credit Unions\/Landers",
  "business_details": [
    {
      "img_url": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/member-logo\/1464667619-pnc.jpg",
      "name": "PNC Bank",
      "des": "PNC offers a wide range of services for all our customers, from individuals and small businesses, to corporations and government entities",
      "address": "",
      "email": "[email protected]",
      "phone": "260-422-5922",
      "member_since": "2016-05-31",
      "img_company": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/member-logo\/1464667619-pnc.jpg",
      "website": "",
      "city": "Fort Wayne",
      "state": "IN",
      "zip": "46808"
    }
  ]
},
{
  "business_category_name": "Cleaning Services",
  "business_details": [
    {
      "img_url": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/uploads\/logo250.png",
      "name": "tsst company",
      "des": "rudurgg ",
      "address": "2005 s calhoun ",
      "email": "",
      "phone": "2602496687",
      "member_since": "2016-05-31",
      "img_company": "http:\/\/edutimeapp.com\/toshow\/chamber-of-commerc\/uploads\/logo250.png",
      "website": "",
      "city": "fort wayne",
      "state": "in",
      "zip": "46825"
    }
  ]
}
 ]
 }

MY code

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURLRequest *req=[[NSURLRequest alloc]initWithURL:[NSURL  URLWithString:@"my url"]];
    response =[[NSMutableData alloc]init];
    [NSURLConnection connectionWithRequest:req delegate:self];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
     NSError *error;
     NSLog(@"Error in receiving data %@",error);
     NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves  error:&error];
     NSLog(@"response data %@",json);
     NSArray *items = [json objectForKey:@"status"];
     pass = [items valueForKey:@"business_category_name"];
     NSLog(@"get category %@",pass);
}
1
  • for more easy, you can create a model class. Commented Jul 6, 2016 at 6:32

3 Answers 3

1

You can do something like,

NSArray *items = [json objectForKey:@"status"];

NSDictionary *firstObjOfItems = [items objectAtIndex:0];

NSString *business_category_name = [firstObjOfItems valueForKey:@"business_category_name"];

NSArray *business_details_Array = [firstObjOfItems objectForKey:@"business_details"];

NSDictionary *first_Object_of_business_details = [business_details_Array objectAtIndex:0]
;

NSString *name = [first_Object_of_business_details valueForKey:@"name"];
NSLog(@"name : %@",name);

You can fetch other parameters likewise. You should use for loop to go through every object. It is just example that how to reach at end element in your json data.

Hope this will help :)

Sign up to request clarification or add additional context in comments.

4 Comments

Fetch only one parameter i want to all name value fetch.
Yes that's why i have mentioned that use for loop to get every name of every object. I think you require two for loops one for items array and one for business_details array and then use objectAtindex : i or j instead of 0
I required name array and name array load in UITableViewCell
Then make one mutable array and add name to that array from every iteration of your for loop and then use that name array as your data source of tableview
0

If you want to fetch only one dictionary from array try this

NSArray *statuses = [json objectForKey:@"status"];

for(NSDictionary *status in statuses){
   NSString *bussiness_category=[status valueForKey:@"business_category_name"];

   NSString *name=[[[status objectForKey:@"business_details"] firstObject] valueForKey:@"name"];

}

If you want to fetch all try this.

Create a mutable array and add all the name inside it and load into tableview like below

NSMutableArray *names = [NSMutableArray new];

NSArray *statuses = [json objectForKey:@"status"];

for(NSDictionary *status in statuses){
   NSString *bussiness_category=[status valueForKey:@"business_category_name"];

   for(NSDictionary *details in [status objectForKey:@"business_details"]){
         NSString *name=[details valueForKey:@"name"];
         [names addObject:name];
   }

}

Hope it helps.

1 Comment

how to string name store in array and load in table view?
0

here dataFromApi is string JSON format;

NSData *objectData = [dataFromApi dataUsingEncoding:NSUTF8StringEncoding];
id collection = [NSJSONSerialization JSONObjectWithData:objectData options:0 error:&error];

after receiving collection you can find data using key value pair.

ex = string add = collection[@"name"];

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.