我想生成以下格式的JSON字符串:
{
"test": {
"currency": "USD",
"gte": "100"
}
}
当我执行以下代码行时:
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[tempArray addObject:tempDict];
[tempArray addObject:subDict];
[dict setObject:tempArray forKey:@"test"];
我检索了这个JSON字符串。
{
"test": [{
"currency": "USD"
}, {
"gte": "100"
}]
}
知道我哪里出错了?
答案 0 :(得分:0)
你必须这样做:
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setObject: @"USD" forKey: @"Currency"];
[tempDict setObject: @"100" forKey: @"gte"];
[dict setObject:tempDict forKey:@"test"];
答案 1 :(得分:0)
从您当前的尝试中,您将字典数组添加到字典中,使其显示为
{"test":[{"currency":"USD"},{"gte":"100"}]}
你需要添加字典, 试试以下,
NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:
tempDict,
subDict,
nil];
NSDictionary *final=@{@"test": dict};
答案 2 :(得分:0)
这个怎么样?
NSMutableDictionary *dic = [@{} mutableCopy];
NSMutableDictionary *tempDic = [@{} mutableCopy];
tempDic[@"currency"] = @"USD";
tempDic[@"gte"] = @"100";
[dic setObject:dic forKey:@"test"];
现代Objective-C风格..
答案 3 :(得分:0)
试试这样: -
NSDictionary *yourDict = @{@"test": @{@"currency": @"USD",@"gte": @"100"}};