gpt4 book ai didi

ios - 使用 nsmutabledictionary 创建嵌套的 JSON 字符串

转载 作者:技术小花猫 更新时间:2023-10-29 11:13:29 28 4
gpt4 key购买 nike

我正在尝试创建以下格式的 json 字符串:

{
"cat_id" : 4992,
"brand" : "Toshiba",
"weight" : { "gte":1000000, "lt":1500000 },
"sitedetails" : {
"name" : "newegg.com",
"latestoffers" : {
"currency": "USD",
"price" : { "gte" : 100 }
}
}
}

我用下面的方法生成这个:

-(void)queryBuilderWithObj:(NSString *)object andKeyValue:(NSString *)key{
NSLog(@"object %@ and key %@",object,key);


if([key rangeOfString:@","].location == NSNotFound){
[querybuild setObject:object forKey:key];
}else{
NSArray *tempArray = [key componentsSeparatedByString:@","];
int index = tempArray.count - 1;
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
while (index) {
if (index == tempArray.count - 1) {
if([querybuild objectForKey:[tempArray objectAtIndex:index]]){
NSMutableArray *objArray = [[NSMutableArray alloc] init];
[objArray addObject:[querybuild objectForKey:[tempArray objectAtIndex:index]]];
[objArray addObject:object];
[tempDict setObject:objArray forKey:[tempArray objectAtIndex:index]];


}else{
[tempDict setObject:object forKey:[tempArray objectAtIndex:index]];
}

}else{
if([querybuild objectForKey:[tempArray objectAtIndex:index]]){

NSMutableArray *objArray = [[NSMutableArray alloc] init];
[objArray addObject:[querybuild objectForKey:[tempArray objectAtIndex:index]]];

NSMutableDictionary *subDict = [[NSMutableDictionary alloc] init];
[subDict setDictionary:tempDict];

[objArray addObject:subDict];
[tempDict setObject:objArray forKey:[tempArray objectAtIndex:index]];



}else{
NSMutableDictionary *subDict = [[NSMutableDictionary alloc] init];
[subDict setDictionary:tempDict];
[tempDict setObject:subDict forKey:[tempArray objectAtIndex:index]];
}
}
index --;
}



[querybuild setObject:tempDict forKey:[tempArray objectAtIndex:0]];

}
NSLog(@"querybuild %@ ",querybuild);


}

最终生成的nsmutabledictionary是:

 querybuild {
brand = Toshiba;
"cat_id" = 4992;
sitedetails = {
gte = 100;
latestoffers = {
gte = 100;
price = {
gte = 100;
};
};
price = {
gte = 100;
};
};
weight = {
lt = 1500000;
};

}

我按如下方式传递了对象和 key :

[sem queryBuilderWithObj:@"4992" andKeyValue:@"cat_id" ];
[sem queryBuilderWithObj:@"Toshiba" andKeyValue:@"brand"];
[sem queryBuilderWithObj:@"1000000" andKeyValue:@"weight,gte"];
[sem queryBuilderWithObj:@"1500000" andKeyValue:@"weight,lt"];
[sem queryBuilderWithObj:@"newegg.com"andKeyValue:@"sitedetails,name" ];
[sem queryBuilderWithObj:@"USD" andKeyValue:@"sitedetails,latestoffers,currency"];
[sem queryBuilderWithObj:@"100" andKeyValue:@"sitedetails,latestoffers,price,gte"];

关于如何生成所需输出的任何想法? querybuild是把这个方法中的nsmutabledictionary对象声明为类变量吗?

最佳答案

如果你想要这个最好的方法是创建更多的字典示例:

NSMutableDictionary *json= [[NSMutableDictionary alloc] init];
[json setObject:@"4992" forKey:@"cat_id"];
[json setObject:@"Toshiba" forKey:@"brand"];
//create weight object
NSMutableDictionary *weight= [[NSMutableDictionary alloc] init];
[weight setObject:@"1000000" forKey:@"gte"];
[weight setObject:@"1500000" forKey:@"lt"];
//attach the object
[json setObject:weight forKey:@"weight"];
//create sitedetails objects
NSMutableDictionary *sitedetails= [[NSMutableDictionary alloc] init];
[sitedetails setObject:@"newegg.com" forKey:@"name"];
//create latestoffers objects
NSMutableDictionary *latestoffers= [[NSMutableDictionary alloc] init];
[latestoffers setObject:@"USD" forKey:@"currency"];
//new dictionary for price
[sitedetails setObject:latestoffers forKey:@"latestoffers"];
[json setObject:sitedetails forKey:@"sitedetails"];
NSLog(@"%@",json);

在你可以将字典转换成json字符串后...

-(NSString*)getJsonStringByDictionary:(NSDictionary*)dictionary{
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
options:NSJSONWritingPrettyPrinted
error:&error];
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

关于ios - 使用 nsmutabledictionary 创建嵌套的 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19614416/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com