ios NSMutableDictionary到JSON转换提供了错误的格式

时间:2013-12-31 08:35:19

标签: ios iphone json nsmutabledictionary

我正在尝试在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"
            }
       ] 
     }

(没有指数,但方括号)请建议。

4 个答案:

答案 0 :(得分:1)

以下示例正是您所需要的。你需要一个数组而不是字典。因为字典中有需要定义的键。

NSMutableArray *offers = [[NSMutableArray 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 addObject:myOffer];
    }

    NSError *errorOffers;
    NSData *jsonDataOffers = [NSJSONSerialization dataWithJSONObject:offers
                                                             options:0
                                                               error:&errorOffers];
    NSString* aStrOffers;
    aStrOffers = [[NSString alloc] initWithData:jsonDataOffers encoding:NSUTF8StringEncoding];

答案 1 :(得分:1)

此代码对您有好处

{"jsonArray":[{"id":"3","quantity":"3","price":"34"},{"id":"3","quantity":"3","price":"34"}]}

NSMutableDictionary *offers = [[NSMutableDictionary alloc] init ];
NSMutableArray *array = [NSMutableArray array];

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"];
    [array addObject:myOffer];
}
[offers setObject:array forKey:@"jsonArray"];

NSError *errorOffers;
NSData *jsonDataOffers = [NSJSONSerialization dataWithJSONObject:offers
                                                         options:0
                                                           error:&errorOffers];
NSString* aStrOffers;
aStrOffers = [[NSString alloc] initWithData:jsonDataOffers encoding:NSUTF8StringEncoding];
NSLog(@"%@",aStrOffers);

答案 2 :(得分:0)

您需要先将数据设置为NSDictionary,然后将其设置为NSMutableArray并使用array NSJSonSerialization,然后您将获得输出正如你所料。

  
      
  • [....] - &gt;表示NSArray对象。
  •   
  • {.....} - &gt;表示NSDictionary对象。
  •   

答案 3 :(得分:0)

NSMutableDictionary *offers = [[NSMutableDictionary alloc] init ];
NSMutableArray *offerDetails = [[NSMutableArray 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"];
    [offerDetails addObject:myOffer];
}

[offers setObject:offerDetails forKey:@"offers"];

NSError *errorOffers;
NSData *jsonDataOffers = [NSJSONSerialization dataWithJSONObject:offers
                                                         options:0
                                                           error:&errorOffers];
NSString* aStrOffers;
aStrOffers = [[NSString alloc] initWithData:jsonDataOffers encoding:NSUTF8StringEncoding];
NSLog(@"%@", aStrOffers);