如何从 mutableArray 访问数组字典值

How to access the array dictionary values from mutableArray

如何从键值数组的字典数组中获取值。

-(void)viewDidLoad{
listMutableArray =[[NSMutableArray alloc] initWithObjects:[NSDictionary dictionaryWithObject:[NSArray 
arrayWithObjects:@"kiran",@"1234512345",@"IN", nil] forKey:[NSArray 
arrayWithObjects:@"name",@"phone",@"location", nil]],nil];
}

// 将值设置为表格视图单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

// Custom cell declaration completed.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}
       NSLog(@"Value of name %@", [[listMutableArray objectAtIndex:indexPath.row] objectForKey:@"name"]);
}

当从 return 零对象的可变数组中打印字典数组中的名称键对时。

如何从可变数组中获取名称 phone 位置值。

您的代码正在创建一个包含单个字典的数组,其中一个数组作为键和值。我想你想要一系列字典;例如:

listMutableArray = [NSMutableArray new];
[listMutableArray addObject:@{
    @"name": @"kiran",
    @"phone": @"1234512345",
    @"location": @"IN"
}];

你的排列方式有误 像这样形成排列方式

NSMutableArray *listMutableArray =[[NSMutableArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"kiran",@"name",@"1234512345",@"phone",@"IN",@"location", nil], nil];

NSLog(@"%@",listMutableArray);

并在 cellForRowAtIndexPath 中尝试打印

NSLog(@"Value of name %@", [[listMutableArray objectAtIndex:indexPath.row] objectForKey:@"name"]);

您想在数组流中添加多个对象,代码如下

NSDictionary *dict = @{
                       @"name": @"kiran",
                       @"phone": @"1234512345",
                       @"location": @"IN"
                       };

NSDictionary *dict1 = @{
                       @"name": @"Bala",
                       @"phone": @"12345123",
                       @"location": @"OUT"
                       };

NSDictionary *dict2 = @{
                       @"name": @"Sri",
                       @"phone": @"898989898",
                       @"location": @"IN"
                       };

NSMutableArray *listMutableArray =[[NSMutableArray alloc] initWithObjects:dict,dict1,dict2, nil];
We get all NSDictionary response add in jsonResult(NSDictionary) and create globel (arrData)NSMutableArray.

arrData = [NSMutableArray new];
for (int i =0; i < [jsonResult count]; i++)
    {
           NSString *string = [NSString stringWithFormat:@"%d",i];
[arrData addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[[jsonResult valueForKey:string] valueForKey:@"name"],@"name",[[jsonResult valueForKey:string] valueForKey:@"phone"],@"phone",[[jsonResult valueForKey:string] valueForKey:@"location"],@"location",nil]];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

// Custom cell declaration completed.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

}
    NSLog(@"Value of name %@", [[arrData objectAtIndex:indexPath.row] valueForKey:@"name"]);
return cell;
}
NSArray *dictionary = @[
                              @{
                                @"Source_lat": @"28.6287",
                                @"Source_lon": @"77.3208",
                                @"Detail":@{
                                            @"Address": @"NOIDA",
                                            @"Region": @"NCR",
                                           },
                                },
                              @{
                                  @"Source_lat": @"28.628454",
                                  @"Source_lon": @"77.376945",

                                  }

                             ];