how to retrieve json data in table view in iOS - ios

I don't know how to retrieve particular data from this JSON data and insert in tableview.
{
"error": "ok",
"data": [{
"facebook_id": "1068644546541665",
"user_id": "95590e92-6c74-44d1-9e5c-5a10c6341ba3",
"name": "Sunil",
"trackingallowed": 1,
"trackingpermission": 1
}, {
"facebook_id": "10311905002886663",
"user_id": "95ab5fc0-9c6d-4e3e-9cb2-bfa4d53e640f",
"name": "Saravanan",
"trackingallowed": 1,
"trackingpermission": 1
}, {
"facebook_id": "998936463528828",
"user_id": "cd6e3c89-bc2d-45a9-8436-9603e1b4724b",
"name": "Suresh",
"trackingallowed": 1,
"trackingpermission": 1
}]
}
Getting the values through List array.
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[friendsconnection_data appendData:data];
NSError *localerror;
NSDictionary *parsedobject =[NSJSONSerialization JSONObjectWithData:data options:0 error:&localerror];
NSLog(#"parsedobject1 %#",parsedobject);
ListArray = [parsedobject valueForKey:#"data"];
NSLog(#"rrresults1 %lu",(unsigned long)ListArray.count);
NSLog(#"results1 %#",ListArray );
}
I am getting EXC_BAD_ACCESS. I need to know how to get particular data from JSON. e.g. I want to get only facebookid and name.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[self FriendsData];
static NSString *TableIdentifier =#"tableitem";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:TableIdentifier];
if(cell == nil)
{
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier];
}
cell.textLabel.text = [[ListArray objectAtIndex:indexPath.row]objectForKey:#"name"];
tableView.rowHeight = 300;
return cell;
}

plz use this code. I think it help you
NSError *json_parse_error;
NSDictionary *responseDictionary=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&json_parse_error];
NSArray *user_info_array=[responseDictionary valueForKey:#"data"];
in your table view delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *TableIdentifier=#"tableitem";;
cell=[tableView dequeueReusableCellWithIdentifier:TableIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:TableIdentifier];
}
cell.textLabel.text = [[user_info_array objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.detailTextLabel.text = [[user_info_array objectAtIndex:indexPath.row] objectForKey:#"facebook_id"];
return cell;
}
//set height for table view cell
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 300.0;
}

Related

Put JSON elements in TableView as Sections Title and Cell title

Below is JSON, and need to put it in table view such that "subitemname" becomes the section title and "subtosubitemname" becomes the title for cell.
I am trying it but didn't get successful.
Title for section is in place but title for rows is not retrieved.
here is the code I tried.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlString = [NSString stringWithFormat:#"URL"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"GET"];
NSError *error;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
NSArray *DATAA=[jsonDict objectForKey:#"Menu"];
subid=[DATAA valueForKey:#"subitemid"];
subname=[DATAA valueForKey:#"subitemname"];
NSArray *sub= [DATAA valueForKey:#"Submenu"];
_SubItemName=[sub valueForKey:#"subtosubitemname"];
_SubItemId=[sub valueForKey:#"subtosubitemid"];
}
#pragma mark - Table view data source
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{ return [subname objectAtIndex:section];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [subid count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_SubItemId count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell=[tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSArray *subn=[_SubItemName objectAtIndex:indexPath.section];
cell.textLabel.text=[subn objectAtIndex:indexPath.row];
return cell;
}
where subname is an array having "subitemname" objects and _SubItemName is an array having "subtosubitemname" objects.
{ "Menu":
[{"subitemid":1,
"itemid":1,
"subitemname":"TELEVISIONS",
"ordernum":1,
"Submenu":
[{
"subitemid":1,
"subtosubitemid":1,
"itemid":1,
"subtosubitemname":"FULL HD TV"},
{
"subitemid":1,
"subtosubitemid":2,
"itemid":1,
"subtosubitemname":"SMART TV"
}
]
},
{
"subitemid":2,
"itemid":1,
"subitemname":"AUDIO & VIDEO",
"ordernum":2,
"Submenu":
[{
"subitemid":2,
"subtosubitemid":3,
"itemid":1,
"subtosubitemname":"HOME AUDIO SYSTEMS"
},
{
"subitemid":2,
"subtosubitemid":4,
"itemid":1,
"subtosubitemname":"DTH SERVICES"
},
{
"subitemid":2,
"subtosubitemid":5,
"itemid":1,
"subtosubitemname":"AUDIO & VIDEO ACCESSORIES"
},
{
"subitemid":2,
"subtosubitemid":11,
"itemid":1,
"subtosubitemname":"PROJECTORS"
}
]
},
{
"subitemid":3,
"itemid":1,
"subitemname":"LARGE APPLIANCES",
"ordernum":3,
"Submenu":
[
{
"subitemid":3,
"subtosubitemid":14,
"itemid":1,
"subtosubitemname":"WASHING MACHINES & DRYERS"
},
{
"subitemid":3,
"subtosubitemid":16,
"itemid":1,
"subtosubitemname":"AIR CONDITIONERS"
},
{
"subitemid":3,
"subtosubitemid":18,
"itemid":1,
"subtosubitemname":"REFRIGERATORS"
},
{
"subitemid":3,
"subtosubitemid":19,
"itemid":1,
"subtosubitemname":"INVERTERS & BATTERIES"
}]}]}
NSMutableArray *menuData = [jsonDict objectForKey:#"Menu"];
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [[menuData objectAtIndex:section] objectForKey:#"subitemname"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [menuData count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *subMenuData = [[menuData objectAtIndex:section] objectForKey:#"Submenu"];
return [subMenuData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell=[tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSDictionary *cellData = [[[menuData objectAtIndex:section] objectForKey:#"Submenu"] objectAtIndex:indexPath.row];
cell.textLabel.text=[cellData objectForKey:#"subtosubitemname"];
return cell;
}
Hope this work for you.
Try this ... Just one array is enough.. menu = [jsonDict objectForKey:#"Menu"];
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return menu[indexPath.section][#"subitemname"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [menu count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *submenu = menu[indexPath.section][#"Submenu"];
return [submenu count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell=[tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSArray *submenu = menu[indexPath.section][#"Submenu"];
cell.textLabel.text = submenu[indexPath.row][#"subtosu‌​bitemname]";
return cell;
}

XML Parsing getting id

how to get all id's from this url and display in uitableview "http://www.thomas-bayer.com/sqlrest/CUSTOMER/".
- (void)viewDidLoad
{
NSURL *URL = [[NSURL alloc] initWithString:#"http://www.thomas-bayer.com/sqlrest/CUSTOMER/"];
NSString *xmlString = [[NSString alloc] initWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:NULL];
xmlDoc = [NSDictionary dictionaryWithXMLString:xmlString];
NSLog(#"dictionary: %#", xmlDoc);
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [xmlDoc count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [[[xmlDoc objectForKey:#"CUSTOMER"]objectAtIndex:indexPath.row]objectForKey:#"__text"];
return cell;
}
I am getting only 0,1,2 values displayed in uitableview and ignoring the remaining id's ?
Use this Library to convert XMLData into NSDictionary object.
https://github.com/nicklockwood/XMLDictionary then simply fetch your required data from NSDictionary object and update it into your TableView.

IOS,Tableview Objective C,

There is 5 elements in an array , in that 2 elements have data and another elements are blank and in tableview it show both element but when i scroll the app crash.i have to show menu_name in the section and its working but when i scroll tableview the app crash because, so can any one give me the solution?
Here is the Response which i have to show in tableview
"status":"true",
"message":"food point menu available",
"data":[
{
"menu_id":"9",
"menu_name":"Morning Menu",
"food_list":[
{
"food_name":"Brigadeiros",
"foodtype":"Pastas",
"food_per_person":"20",
"food_bonus":"packing"
},
{
"food_name":"Cachaca",
"foodtype":"Pastas",
"food_per_person":"30",
"food_bonus":"packing"
}
]
},
{
"menu_id":"10",
"menu_name":"Afternoon Menu",
"food_list":[
{
"food_name":"Quindim",
"foodtype":"Pastas",
"food_per_person":"30",
"food_bonus":"packing"
},
{
"food_name":"Cachaca",
"foodtype":"Pastas",
"food_per_person":"30",
"food_bonus":"packing"
}
]
},
{
"menu_id":"12",
"menu_name":"Evening Menu",
"food_list":""
},
{
"menu_id":"17",
"menu_name":"MenuSegundaFeira",
"food_list":""
},
{
"menu_id":"18",
"menu_name":"MenuTercaFeira",
"food_list":""
}
]
}
And this is the code which i have written in my app
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([FinalArrayCategory count]>0) {
return [[[FinalArrayCategory objectAtIndex:0] objectForKey:#"food_list"] count];
}
else
{
return 0;
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if([FinalArrayCategory count]>0) {
return [[FinalArrayCategory valueForKey:#"menu_name"] count];
}
else
{
return 0;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionName;
sectionName =[[FinalArrayCategory valueForKey:#"menu_name"] objectAtIndex:section];
return sectionName;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MenuListCELL *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.lblpice.text=[[[[FinalArrayCategory objectAtIndex:indexPath.section]objectForKey:#"food_list"] valueForKey:#"food_name"]objectAtIndex:indexPath.row];
// cell.lblType.text=[[FinalArrayCategory valueForKey:#"category_name"] objectAtIndex:indexPath.row];
// cell.lblBonus.text=[[FinalArrayCategory valueForKey:#"food_bonus"] objectAtIndex:indexPath.section];
// cell.lblFoodName.text=[[FinalArrayCategory valueForKey:#"menu_name"] objectAtIndex:indexPath.section];
//
// NSString *pickURL=[[FinalArrayCategory valueForKey:#"food_image"] objectAtIndex:indexPath.section];
// [cell.lblImage sd_setImageWithURL:[NSURL URLWithString:pickURL] placeholderImage:[UIImage imageNamed:#"itemplaceholder.jpg"] options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
// }];
return cell;
}
return [[[FinalArrayCategory objectAtIndex:0] objectForKey:#"food_list"] count];
change objectAtIndext from 0 to IndexPath.section
Please try this code..
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[FinalArrayCategory objectAtIndex:section] objectForKey:#"food_list"] count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [FinalArrayCategory count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionName;
sectionName =[[FinalArrayCategory objectAtIndex:section] objectForKey:#"menu_name"];
return sectionName;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MenuListCELL *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ([[[FinalArrayCategory objectAtIndex:indexPath.section] objectForKey:#"food_list"] iskindOfClass:[NSArray class]]) {
NSDictionary *dictData=[[[FinalArrayCategory objectAtIndex:indexPath.section] objectForKey:#"food_list"] objectAtIndex:indexPath.row];
cell.lblpice.text=[dictData objectForKey:#"food_name"];
}else{
cell.lblpice.text=#"";
}
return cell;
}
And let me know its working or not.
Happy Coding!!
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MenuListCELL *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if([FinalArrayCategory objectAtIndex:indexPath.section]objectForKey:#"food_list"].count>0){
NSString *foodName =[[[[FinalArrayCategory objectAtIndex:indexPath.section]objectForKey:#"food_list"] valueForKey:#"food_name"]objectAtIndex:indexPath.row];
if(foodName){
cell.lblpice.text=foodName;
}
}else{
cell.lblpice.text=#"";
}
return cell;
}
please try this

I am trying to display response of json in UITableViewCell but not working

Hi I am trying to display response like in this image but not display and some error are occurs so please help me
-(void)getCategories
{
Service *srv=[[Service alloc]init];
NSString *str=#"http://streamtvbox.com/site/api/matrix/";//?country_code=91&phone=9173140993&fingerprint=2222222222";
NSString *method=#"channels";
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[srv postToURL:str withMethod:method andParams:dict completion:^(BOOL success, NSDictionary *responseObj)
{
NSLog(#"%#",responseObj);
arrayCategories = [responseObj valueForKey:#"categories"];
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrayCategories.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"ChannelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [[arrayCategories objectAtIndex:indexPath.row]valueForKey:#"categories"];
return cell;
}
After you receive the response please reload,
[srv postToURL:str withMethod:method andParams:dict completion:^(BOOL success, NSDictionary *responseObj)
{
NSLog(#"%#",responseObj);
arrayCategories = [responseObj valueForKey:#"categories"];
[youTableView reloadDate];
}];

Count number of values in dictionary object

<pre>
products = (
{
id = 19;
"image_url" = "http://localhost:8888/straightoffer/image/data/1330374078photography.jpg";
name = "Save $240 on your next photo session";
},
{
id = 21;
"image_url" = "http://localhost:8888/straightoffer/image/data/1330373696massage.jpg";
name = "One Hour Massage";
}
);
}
</pre>
the above is what I got through json, I need to assign the values to uitableviews:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(#"Number of rows: %d",[[products objectForKey:#"products"] count]);
return [[products objectForKey:#"products"] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"cell"];
}
NSString *currentProductName;
currentProductName = [productKeys objectAtIndex:indexPath.row];
NSLog(#"product name : %#", currentProductName);
[[cell textLabel] setText:currentProductName];
return cell;
}
it returns 0 number of rows, I am newbie to ios please help me how I will assign these values to uitableview.
Regards
The problem is that what you posted is not json. It should be
{
"products":[
{
"id":19,
"image_url":"http://localhost:8888/straightoffer/image/data/1330374078photography.jpg",
"name":"Save $240 on your next photo session"
},
{
"id":21,
"image_url":"http://localhost:8888/straightoffer/image/data/1330373696massage.jpg",
"name":"One Hour Massage"
}
]
}
You can use http://jsonlint.com/ to validate your json files.
I have used the above json file and did the following:
NSBundle* bundle = [NSBundle mainBundle];
NSString *jsonPath = [bundle pathForResource:#"data" ofType:#"json"];
NSData *data = [NSData dataWithContentsOfFile:jsonPath];
NSError *error;
NSDictionary *products = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
DLog(#"products: %i", [[products objectForKey:#"products"] count] );
[self.tableView reloadData];
Result is: products: 2.
You should be able to reproduce this.

Resources