Reputation: 5420
I have this json
{
"title": "xyz",
"category": "Monetary",
"target": "55",
"achieve": "0",
"todolist": [
{
"todoitem": "one"
},
{
"todoitem": "two"
},
{
"todoitem": "three"
}
]
}
coming from API and I want to add todolist array to
In .h file
@property (strong, nonatomic) NSMutableArray *todolist;
@property (strong, nonatomic) NSMutableArray *todolists;
@property (strong, nonatomic) NSMutableArray *listnumber;
In .m file
todolist=[[NSMutableArray alloc] initWithObjects: nil];
todolists=[[NSMutableArray alloc] initWithObjects: nil];
listnumber=[[NSMutableArray alloc] initWithObjects: nil];
In function getting json
todolists = [result valueForKey:@"todolist"];
for (int j =0; j < todolist.count; j++)
{
[todolist addObject:[[todolists objectAtIndex:j] valueForKey:@"todoitem"]];
[listnumber addObject:[NSString stringWithFormat:@"%d", j]];
}
[tvToDoList reloadData];
CellForRowAtIndexPath I am adding values two field
static NSString *CellIdentifier=@"Cell";
TodolistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell==nil)
{
cell = [[TodolistTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
int row = (int)[indexPath row];
cell.valListitem.text = todolist[row];
cell.lblNo.text = listnumber[row];
return cell;
}
where result is containing the whole json
Upvotes: 0
Views: 282
Reputation: 16446
If you want to get string from array here is solution
for (int i =0; i < listnumber.count; i++)
{
[todolist addObject:[[listnumber objectAtIndex:i] valueForKey:@"todoitem"]];
}
Upvotes: 1
Reputation: 1742
Try this in one line code.
todolist = [[[result valueForKey:@"todolist"] valueForKey:@"todoitem"] mutableCopy];
Upvotes: 4
Reputation: 2745
Try this
@property (strong, nonatomic) NSMutableArray *todolist;
todolist=[[NSMutableArray alloc] initWithObjects: nil];
//Convert to NSData
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
//JSON object
id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//Array of ToDo List
NSArray *list = (NSArray *)[object valueForKey:@"todolist"];
//Fetch items of ToDo List
for (int i =0; i < [list count]; i++)
{
[todolist addObject:[[list objectAtIndex:i] valueForKey:@"todoitem"]];
}
NSLog(@"Array :: %@", todolist);
Hopefully, this will help you.
Thanks.
Upvotes: 0
Reputation: 3529
Parse your json string and get the todos as NSArray from that.
@Try this
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
id object = [NSJSONSerialization
JSONObjectWithData:data
options:0
error:&error];
NSArray *todos = [object valueForKey:@"todolist"];
NSMutableArray *mutableToDos = [[NSMutableArray alloc] initWithArray:todos];
Here is the code for display in cell
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *todo = [mutableToDos objectAtIndex:indexPath.row];
[[cell textLabel] setText:[todo valueForKey:@"todoitem"];
return cell;
}
Upvotes: 0