A.S.K A.S.K - 5 months ago 14
JSON Question

how to parse the JSON response in the Custom tableview

My structure of JSON is

{"Offer_Stag1":
[{
"Area":"1200م2",
"HotlineNumber1":"12345678",
"HotlineNumber2":"12345678",
"Image":"almithaq.mawaqaademo.com\/AlMithaqOfferThumbImages\/19d92aa8-fbc7-49fc-9ebe-a4bdc641216b.jpg",
"LargeImage":"almithaq.mawaqaademo.com\/AlMithaqOfferThumbImages\/81ecfc05-8431-4ce7-8ac9-5e93c24d7deb.jpg",
"SectionNo":"184","Stage":"Stage1",
"TheDetails":"موقع مميز، على مدخل المرحلة الاولى، قريبة من الخدمات\u0009","TheView":" البحرية 17 متر"
},
{"Area":"1205م2 – 1201م2",
"HotlineNumber1":"12345678",
"HotlineNumber2":"12345678",
"Image":"almithaq.mawaqaademo.com\/AlMithaqOfferThumbImages\/9cdcab19-49f8-4ad8-a42c-f5e9df273320.jpg",
"LargeImage":"almithaq.mawaqaademo.com\/AlMithaqOfferThumbImages\/bca29ff9-e6f3-4554-8a8c-9f685ceb9ba6.jpg",
"SectionNo":"581-582",
"Stage":"Stage1",
"TheDetails":"أرضين متلاصقتين، سكة جانبية","TheView":"البحرية 16 متر"
}]
}


i have customized table cell want to parse it, hlep me out.

Thanks in Advance.

Answer
self.itemshowdetailsAr=[[NSMutableArray alloc] init];

-(void)yourmethodName
{
aray = [[NSArray alloc] init];

NSURL *url = [[NSURL alloc] initWithString:@"yoururlname.php"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"the json resut is %@",JSON);
    aray = [JSON objectForKey:@"Offer_Stag1"];


        for (NSDictionary *tmp in aray) {
        NSMutableDictionary   *itemshowdetails=[[NSMutableDictionary alloc]init];

            [itemshowdetails setValue:[tmp objectForKey:@"Area"] forKey:@"Area"];
            [itemshowdetails setValue:[tmp objectForKey:@"HotlineNumber1"] forKey:@"HotlineNumber1"];
            [itemshowdetails setValue:[tmp objectForKey:@"HotlineNumber2"] forKey:@"HotlineNumber2"];
            [itemshowdetails setValue:[tmp objectForKey:@"TheDetails"] forKey:@"TheDetails"];
            [itemshowdetails setValue:[tmp objectForKey:@"TheView"] forKey:@"TheView"];


            [self.itemshowdetailsAr addObject:itemshowdetails];
        }

    [self.tableView reloadData];  

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];

[operation start];
}


#pragma mark - tablevie data source

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [self.itemshowdetailsAr count];

}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
static NSString *CellIdentifier = @"cell";


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];




if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;


}



  cell.textLabel.text = [[self.itemshowdetailsAr objectAtIndex:indexPath.row ] objectForKey:@"Area"];

cell.detailTextLabel.text = [[self.itemshowdetailsAr objectAtIndex:indexPath.row ] objectForKey:@"HotlineNumber1"];
 ....
 ....
 .....
return cell;

 }
Comments