Pregunta

I am trying to convert a NSMutableDictionary to json on ios.

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

I am getting the result.

"offers": {
            "0": {
                "id": "3",
                "quantity": "3",
                "price": "34"
            },
            "1": {
                "id": "3",
                "quantity": "3",
                "price": "34"
            }   
  }

But I need it as

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

(no indices , but square bracket ) Please advice.

¿Fue útil?

Solución

The example below does exactly what you need. You need an array instead of a dictionary. Because a dictionary has keys that need to be defined.

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

Otros consejos

This code is good for you

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

You need to set the data to the NSDictionary first and then make that to the NSMutableArray and use the array for the NSJSonSerialization , then you will get the output as you are Expected.

  • [.... ] --> Indicates the NSArray objects.
  • {.....} --> Indicates the NSDictionary objects.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top