繁体   English   中英

ios NSMutableDictionary到JSON转换格式错误

[英]ios NSMutableDictionary to JSON convertion gives wrong format

我正在尝试将NSMutableDictionary转换为ios上的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"
            }
       ] 
     }

(无索引,但方括号)请指教。

下面的示例完全满足您的需求。 您需要一个数组而不是字典。 因为字典具有需要定义的键。

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];

该代码对您有好处

{"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);

您需要先将数据设置为NSDictionary ,然后将其设置为NSMutableArray并将该array用于NSJSonSerialization ,然后将按预期获得输出。

  • [....]->表示NSArray对象。
  • {.....}->表示NSDictionary对象。
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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM