我正在尝试创建以下格式的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;
};
}
我传递了对象和密钥,如下所示:
[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对象吗?
答案 0 :(得分:11)
如果你想要这个,最好的方法是创建更多字典示例:
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];
}
答案 1 :(得分:7)
使用新的Objective-C语法和NSJSONSerialization
,正如其他人所指出的,这可以很好地完成:
NSDictionary *latestprice = @{@"gte": @100};
NSDictionary *latest = @{@"currency": @"USD", @"price": latestprice};
NSDictionary *details = @{@"name": @"newegg.com", @"latestoffers": latest};
NSDictionary *weight = @{@"gte": @1000000, @"lt": @1500000};
NSDictionary *json = @{
@"cat_id": @4992,
@"brand": @"Toshiba",
@"weight": weight,
@"sitedetails": details
};
NSData *data = [NSJSONSerialization dataWithJSONObject:json
options:NSJSONWritingPrettyPrinted
error:nil];
NSString *jsonStr = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
我不知道您的 querybuild 是什么,但如果它是字典,您可以根据需要提取数据。或者您是否需要动态根据querybuild中的键创建此结构?
答案 2 :(得分:2)
您是否尝试过使用NSJSONSerialization类的JSONObjectWithData:options:error:
?
它还提供了一个方便的isValidJSONObject
方法,可用于帮助您调试您尝试转换的结构。
答案 3 :(得分:0)
没必要。只需使用内置的NSJSONSerialization
,因为它就是它的用途。它会将JSON
个对象放入NSDictionaries
或NSArrays
。
以下是您如何使用它:
//This part is just to download the data. If you're using another method - that's fine. Just make sure that the download is in NSData format
NSURL *url = [[NSURL alloc] initWithString : @"YOUR_URL_HERE"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL : url];
NSData *jsonData = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil
error:nil];
//This is the actual NSJSONSerialization part.
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableLeaves
error:nil];
现在您可以像这样访问NSDictionary
中的元素:
NSString *cat_id = [jsonDict objectForKey : @"cat_id"];
{ }
中的那些对象是NSDictionaries
- 嵌套的对象。
他们可以访问:
NSDictionary *slidedetailsDict = [jsonDict objectForKey : @"slidedetails"];
NSString *name = [slidedetailsDict objectForKey : @"name"];