这是我尝试过的JSON和源代码:
JSON:
{
"total": 2,
"floorplans": [
{
"_id": {
"$id": "57fdd13e368e728c278328f6"
},
"indoor_map_name": “Building 1”,
"geo_coords": {
"lat": "42.0912000000000010",
"long": "-71.2643390000000070"
},
"map_community_id": 20316,
"map_drawing_id": 31728,
"map_level_id": 53146,
"organization": [
"org-57d67a986edc6182458466"
],
"drawings": [
{
"map_drawing_id": "21419",
"levels": [
{
"map_level_id": "53136",
"min_height": "70",
"max_height": "75"
}
]
},
{
"map_drawing_id": "31728",
"levels": [
{
"map_level_id": "53146",
"min_height": "70",
"max_height": "86"
},
{
"map_level_id": "53149",
"min_height": "86",
"max_height": "96"
},
{
"map_level_id": "53150",
"min_height": "96",
"max_height": "110"
}
]
}
],
"uuid": "mapgroup-57fdd13e25afd890493911"
}
]
}
码:
-(void)getAllDrawingsandLevels:(NSDictionary *)dict
{
int total = [[dict objectForKey:@"total"] intValue];
if (total >0) {
arrFloorplans =[dict objectForKey:@"floorplans"];
if (arrFloorplans!=nil) {
for (int i=0; i<[arrFloorplans count]; i++) {
arrFloorplans1 = [arrFloorplans objectAtIndex:i];
//NSLog(@"arrDrawings1 %@",arrFloorplans1);
NSString *srtCommID = [arrFloorplans1 valueForKey:@"map_community_id"];
if ([[NSString stringWithFormat:@"%@",srtCommID] isEqualToString:[NSString stringWithFormat:@"%@",[kCommunityid objectAtIndex:i]]]) {
arrDrawings = [arrFloorplans1 valueForKey:@"drawings"];
if (arrDrawings !=nil) {
for (int j = 0; j<[arrDrawings count]; j++) {
arrDrawings1 = [arrDrawings objectAtIndex:j];
NSString *StrDrwaingID = [NSString stringWithFormat:@"%@",[arrDrawings1 valueForKey:@"map_drawing_id"]];
[arrDrawingsID addObject:StrDrwaingID];
//[arrLevelsID removeAllObjects];
arrLevels = [arrDrawings1 valueForKey:@"levels"];
for (int k = 0; k< [arrLevels count]; k++) {
arrLevels1 = [arrLevels objectAtIndex:k];
NSString *StrLevelID = [NSString stringWithFormat:@"%@",[arrLevels1 valueForKey:@"map_level_id"]];
[arrLevelsID addObject:StrLevelID];
}
[arrFinalLevelID addObjectsFromArray:arrLevelsID];
}
NSLog(@"arrDrawingsID %@",arrDrawingsID);
NSLog(@"arrLevelsID %@",arrLevelsID);
NSLog(@"arrFinalLevelID %@",arrFinalLevelID);
}
}
}
}
}
我想要两个数组
1)arrDrawingsID =
[21419][31728]
我成功获得了第二个2)arrFinalLevelID =
[53136][53146,53149,53150]
但是使用此代码,我的输出是[53136,53136,53146,53149,53150]
提前致谢,
米希尔
最佳答案
问题是在这行[arrFinalLevelID addObjectsFromArray:arrLevelsID];
中应该是[arrFinalLevelID addObject:arrLevelsID];
,还需要在循环之前重新初始化arrLevelsID
。
arrLevelsID = [NSMutableArray alloc] init]; //reinitialized Array for removing previous value
arrLevels = [arrDrawings1 valueForKey:@"levels"];
for (int k = 0; k< [arrLevels count]; k++) {
arrLevels1 = [arrLevels objectAtIndex:k];
NSString *StrLevelID = [NSString stringWithFormat:@"%@",[arrLevels1 valueForKey:@"map_level_id"]];
[arrLevelsID addObject:StrLevelID];
}
[arrFinalLevelID addObject:arrLevelsID]; //instead of addObjectsFromArray: