How to get JSON response array all index values?

前端 未结 2 1941
深忆病人
深忆病人 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 14:49
    NSString *strName = (NSString *) [yourArray valueForKey:@"name"];
    NSInteger age = [(NSNumber *) [yourArray valueForKey:@"age"] integerValue];
    
    0 讨论(0)
  • 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;
        }
    )
    
    0 讨论(0)
提交回复
热议问题