你好,我正在研究一个项目,我必须以这种格式创建一个数组:
{
"1": {
"Child_Gender": "",
"Child_DOB": "",
"Child_tobbacoUse": ""
},
"2": {
"Child_Gender": "",
"Child_DOB": "",
"Child_tobbacoUse": ""
},
但我创建的数组提供了这种格式
(
{
"Child_DOB" = "";
"Child_Gender" = "";
"Child_tobbacoUse" = "";
},
{
"Child_DOB" = "";
"Child_Gender" = "";
"Child_tobbacoUse" = "";
}
)
我正在使用以下代码:
for (int i = 0; i < arrayChildModel.count; i++)
{
ChildModel *model = [arrayChildModel objectAtIndex:i];
NSString *keyOrder = [NSString stringWithFormat:@"Children_Gender[%d]",i];
NSString *keyQuantity = [NSString stringWithFormat:@"Children_BirthDate[%d]",i];
NSString *keyTobacco = [NSString stringWithFormat:@"Children_HasTobaccoUsage[%d]",i];
//NSString *keyNotes = [NSString stringWithFormat:@"row[%d][description]",i];
childDateOfBirthStr = model.ChildBirthDate ;
childGenderStr = model.ChildGender ;
childTobaccoStr = model.ChildTobaccoUsage;
NSString *string1 = [NSString stringWithFormat:@"%@=%@&",keyOrder,childGenderStr];
NSString *string2 = [NSString stringWithFormat:@"%@=%@",keyQuantity,childDateOfBirthStr];
NSString *string3 = [NSString stringWithFormat:@"%@=%@",keyTobacco,childTobaccoStr];
[stringParams appendString:string1];
[stringParams appendString:string2];
[stringParams appendString:string3];
NSDictionary * ChildDict;
NSArray * ApplicantKeys = [[NSArray alloc]initWithObjects:@"Child_Gender",@"Child_DOB",@"Child_tobbacoUse", nil];
NSArray * ApplicantObjects = [[NSArray alloc]initWithObjects:childGenderStr,childDateOfBirthStr,childTobaccoStr, nil];
ChildDict = [NSDictionary dictionaryWithObjects:ApplicantObjects forKeys:ApplicantKeys];
NSLog(@"Dictionary is %@",ChildDict);
[CompleteArray addObject:ChildDict];
}
NSLog(@"Final Array is %@",CompleteArray);
请帮我将输出数组转换为所需的格式化数组
答案 0 :(得分:0)
为循环创建一个字典外部
NSMutableDictionary *dict =[[NSMutableDictionary alloc]init];
在NSLog(@"Dictionary is %@",ChildDict);
[dict setObject:ChildDict forKey:[NSString stringWithFormat:@"%ld",i+1]];
[CompleteArray addObject:dict];
答案 1 :(得分:0)
请检查以下解决方案,它将为您提供预期的输出。
{{1}}