0

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

10
  • 2
    What's the problem? What have you tried? Where is the error? Commented May 20, 2015 at 5:12
  • do you think its a right way to do this ? Commented May 20, 2015 at 5:13
  • This site is not for code review. Commented May 20, 2015 at 5:15
  • @VarunNaharia Assign listNumber as a NSArray. and You can then easily create mutable array from it. Commented May 20, 2015 at 5:19
  • there is no error but list dose not load in table view and I am not asking you to review my code Commented May 20, 2015 at 5:19

4 Answers 4

4

Try this in one line code.

todolist = [[[result valueForKey:@"todolist"] valueForKey:@"todoitem"] mutableCopy];
Sign up to request clarification or add additional context in comments.

3 Comments

Good Logic. Good answer.
you didn't add addObject app is crash
but +1 for your mutableCopy
1

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"]];
}

4 Comments

not working and there is some warning ` Ordered comparison between pointer and interger('int' and 'NSMutableArray *')` and its an infinity loop
sorry i made some mistake there just update in for loop i < listnumber.count where i<listnumber
app crash by this [__NSCFDictionary length]: unrecognized selector sent to instance 0x7ff131ee8dc0
make sure you get data in array format in listnumber just put debug point there and in for loop and let me know where it crashes
0

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;
}

1 Comment

did you check with debugger the array contains the value or not?
0

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.

1 Comment

simply parse the json string and use it

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.