【问题标题】:ios NSMutableDictionary to JSON convertion gives wrong formatios NSMutableDictionary 到 JSON 的转换给出了错误的格式
【发布时间】: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


【解决方案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];

【讨论】:

    【解决方案2】:

    这段代码对你有好处

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

    【讨论】:

      【解决方案3】:
      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);
      

      【讨论】:

        【解决方案4】:

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

        • [....] --> 表示 NSArray 对象。
        • {.....} --> 表示 NSDictionary 对象。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-25
          • 1970-01-01
          相关资源
          最近更新 更多