How to get JSON response array all index values?

前端 未结 2 1942
深忆病人
深忆病人 2021-01-29 14:24

I need to get JSON response array all indexes values and maintain separate array. Here below I have posted my JSON response and I wanted to get console output looks like below a

2条回答
  •  醉话见心
    2021-01-29 15:02

    if you want to get all objects, get the array using the respective key.Then store the result in another array by iterating the for loop according to array count.

    NSArray *recordsArr = [[responsedata objectAtIndex:0] valueForKey:@"A"];
    
    NSMutableArray *resultArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < [recordsArr count]; i ++) {
        NSMutableDictionary *recordDict = [[NSMutableDictionary alloc] init];
    
        [recordDict setObject:[[recordsArr objectAtIndex:i] valueForKey:@"name"] forKey:@"name"];
        [recordDict setObject:[[[recordsArr objectAtIndex:i] valueForKey:@"age"] objectAtIndex:0] forKey:@"age"];
    
        [resultArray addObject:recordDict];
    }
    
    NSLog(@"%@",resultArray);
    

    output:

    2014-09-18 12:49:22.047 testprj[1044:60b] (
            {
            age = 4;
            name = sons;
        },
            {
            age = 2;
            name = rondo;
        }
    )
    

提交回复
热议问题