【发布时间】:2013-12-31 08:35:19
【问题描述】:
我正在尝试在 ios 上将 NSMutableDictionary 转换为 json。
NSMutableDictionary *offers = [[NSMutableDictionary alloc] init ];
for(int i=0;i<2;i++) {
NSMutableDictionary *myOffer = [[NSMutableDictionary alloc]init];
[myOffer setObject:@"3" forKey:@"id"];
[myOffer setObject:@"34" forKey:@"price"];
[myOffer setObject:@"3" forKey:@"quantity"];
[offers setObject:myOffer forKey:[NSString stringWithFormat:@"%i",i]];
}
NSError *errorOffers;
NSData *jsonDataOffers = [NSJSONSerialization dataWithJSONObject:offers
options:0
error:&errorOffers];
NSString* aStrOffers;
aStrOffers = [[NSString alloc] initWithData:jsonDataOffers encoding:NSUTF8StringEncoding];
我得到了结果。
"offers": {
"0": {
"id": "3",
"quantity": "3",
"price": "34"
},
"1": {
"id": "3",
"quantity": "3",
"price": "34"
}
}
但我需要它作为
"offers": {
[ {
"id": "3",
"quantity": "3",
"price": "34"
},
{
"id": "3",
"quantity": "3",
"price": "34"
}
]
}
(无索引,但方括号)请指教。
【问题讨论】:
-
您是否尝试过将
*offers设为NSMutableArray而不是字典?这应该可以满足您的需求。
标签: ios iphone json nsmutabledictionary