如何在NSMutable Array中添加包含数组的字符串?

时间:2015-05-20 05:10:29

标签: ios objective-c json nsmutablearray

我有这个json

{
    "title": "xyz",
    "category": "Monetary",
    "target": "55",
    "achieve": "0",
    "todolist": [
        {
            "todoitem": "one"
        },
        {
            "todoitem": "two"
        },
        {
            "todoitem": "three"
        }
    ]
}

来自API,我想将todolist数组添加到

在.h文件中

@property (strong, nonatomic) NSMutableArray *todolist;
@property (strong, nonatomic) NSMutableArray *todolists;
@property (strong, nonatomic) NSMutableArray *listnumber;

在.m文件中

todolist=[[NSMutableArray alloc] initWithObjects: nil];
todolists=[[NSMutableArray alloc] initWithObjects: nil];
listnumber=[[NSMutableArray alloc] initWithObjects: nil];

在函数中获取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我正在添加值两个字段

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

其中result包含整个json

4 个答案:

答案 0 :(得分:4)

在一行代码中尝试此操作。

todolist = [[[result valueForKey:@"todolist"] valueForKey:@"todoitem"] mutableCopy];

答案 1 :(得分:1)

如果你想从数组中获取字符串,那就是解决方案

for (int i =0; i < listnumber.count; i++) 
{
  [todolist addObject:[[listnumber objectAtIndex:i] valueForKey:@"todoitem"]];
}

答案 2 :(得分:0)

解析你的json字符串并从那里获得 NSArray 的待办事项。

@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];

以下是在单元格中显示的代码

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

答案 3 :(得分:0)

试试这个

@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);

希望这会对你有所帮助。
感谢。