aakash aakash - 6 months ago 10
Objective-C Question

Show array data into one dictionary?

I am having this parse data

hello {"response":"200","category-list":true,"category":[{"prod_id":"1","prod_name":"Apparel & Accessories","prod_status":"1","prod_order":"1","prod_slug":"apparel-accessories"},{"prod_id":"2","prod_name":"Automotive Services","prod_status":"1","prod_order":"2","prod_slug":"automotive-services"},{"prod_id":"12","prod_name":"Building and Construction","prod_status":"1","prod_order":"11","prod_slug":"building-and-construction"},{"prod_id":"9","prod_name":"Business & Professional Services","prod_status":"1","prod_order":"8","prod_slug":"business-professional-services"},{"prod_id":"10","prod_name":"Business Services","prod_status":"1","prod_order":"9","prod_slug":"business-services"},{"prod_id":"11","prod_name":"Community & Education","prod_status":"1","prod_order":"10","prod_slug":"community-education"},{"prod_id":"13","prod_name":"Education","prod_status":"1","prod_order":"12","prod_slug":"education"},{"prod_id":"14","prod_name":"Entertainment & Media","prod_status":"1","prod_order":"13","prod_slug":"entertainment-media"},{"prod_id":"15","prod_name":"Family & Community","prod_status":"1","prod_order":"14","prod_slug":"family-community"},{"prod_id":"16","prod_name":"Finance","prod_status":"1","prod_order":"15","prod_slug":"finance"},{"prod_id":"17","prod_name":"Finance & Legal","prod_status":"1","prod_order":"16","prod_slug":"finance-legal"},{"prod_id":"18","prod_name":"Food & Dining","prod_status":"1","prod_order":"17","prod_slug":"food-dining"},{"prod_id":"3","prod_name":"FREIGHT","prod_status":"1","prod_order":"3","prod_slug":"bgdr"},{"prod_id":"19","prod_name":"Health & Medicine","prod_status":"1","prod_order":"18","prod_slug":"health-medicine"},{"prod_id":"20","prod_name":"Home & Garden","prod_status":"1","prod_order":"19","prod_slug":"home-garden"},{"prod_id":"21","prod_name":"Home Repair & Improvement","prod_status":"1","prod_order":"20","prod_slug":"home-repair-improvement"},{"prod_id":"22","prod_name":"Industrial supplies & services","prod_status":"1","prod_order":"21","prod_slug":"industrial-supplies-services"},{"prod_id":"23","prod_name":"Information Technology","prod_status":"1","prod_order":"22","prod_slug":"information-technology"},{"prod_id":"24","prod_name":"Legal","prod_status":"1","prod_order":"23","prod_slug":"legal"},{"prod_id":"7","prod_name":"My New Category","prod_status":"1","prod_order":"6","prod_slug":"new-category"},{"prod_id":"8","prod_name":"My Testing Category","prod_status":"1","prod_order":"7","prod_slug":"my-testing-category"},{"prod_id":"4","prod_name":"PELLETS","prod_status":"1","prod_order":"4","prod_slug":"gdgggdsgdfgdg"},{"prod_id":"25","prod_name":"Personal Care","prod_status":"1","prod_order":"24","prod_slug":"personal-care"},{"prod_id":"26","prod_name":"Public utilities & environment","prod_status":"1","prod_order":"25","prod_slug":"public-utilities-environment"},{"prod_id":"27","prod_name":"Real Estate","prod_status":"1","prod_order":"26","prod_slug":"real-estate"},{"prod_id":"28","prod_name":"Real Estate & Insurance","prod_status":"1","prod_order":"27","prod_slug":"real-estate-insurance"},{"prod_id":"29","prod_name":"Shopping","prod_status":"1","prod_order":"28","prod_slug":"shopping"},{"prod_id":"30","prod_name":"Shopping & Specialty Stores","prod_status":"1","prod_order":"29","prod_slug":"shopping-specialty-stores"},{"prod_id":"31","prod_name":"Sports & Recreation","prod_status":"1","prod_order":"30","prod_slug":"sports-recreation"},{"prod_id":"6","prod_name":"TRANSPORT SERVICES","prod_status":"1","prod_order":"5","prod_slug":"transport"},{"prod_id":"32","prod_name":"Transportation","prod_status":"1","prod_order":"31","prod_slug":"transportation"},{"prod_id":"33","prod_name":"Travel","prod_status":"1","prod_order":"32","prod_slug":"travel"},{"prod_id":"34","prod_name":"Travel & Lodging","prod_status":"1","prod_order":"33","prod_slug":"travel-lodging"},{"prod_id":"35","prod_name":"Uncategorized","prod_status":"1","prod_order":"34","prod_slug":"uncategorized"}]}

i have tried this :-

- (IBAction)SelectCategory:(id)sender
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:@"http://d.com/category"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"hello %@",str);

NSDictionary *dict1 = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];

categoryArray =[[NSArray alloc]init];

CategoryDict = [[NSMutableDictionary alloc]init];
categoryArray = [dict1 allValues];
CategoryDict = [[categoryArray objectAtIndex:0]valueForKey:@"prod_name"];

NSLog(@"category dict = %@",CategoryDict);
[CategoryTable reloadData];
CategoryTable.hidden = FALSE;


}


How to get "prod_name" data into
cell
of
UITableView
?
thanks In advance.

Answer

do like

on your ViewDidLoad

 @interface XXXXViewController ()
{
 NSMutableArray *categoryArray;
}

 - (void)viewDidLoad
 {
[super viewDidLoad];
 categoryArray =  [[NSMutableArray alloc]init];
  }

  NSDictionary *dict1 = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];

  categoryArray  =  [dict1 valueForKey:@"category"] ;  

    if (categoryArray.count > 0)      
    {
     CategoryTable.hidden = FALSE;
    [CategoryTable reloadData];
    }
}

and access LIke

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [categoryArray count];
}

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 cell.textLabel.text=[NSString stringWithFormat:@"%@ ",[[categoryArray objectAtIndex:indexPath.row] objectForKey:@"prod_name"]];
   }