0

I am trying to send an array of custom objects to a server using JSON.

I have tried two approaches. One I stringify the array of objects and send it.

NSArray <Offers *> *offers = [self getOffers];
NSString *offersString = [offers componentsJoinedByString:@","];

This gets it to the server but on the server end, I end up with an incredibly complicated string that makes reconverting in to structured data on the server which uses PHP a challenge. Also since something could go awry with the string in transmission, it would seem to be a fragile approach.

The second approach is to serialize the array of objects into a JSONData object on the phone and send it. The problem with this is that I also want to send some other data such as an authentication string/token And I don't know how to assemble a JSONData object that contains the authentication string on the one hand and the JSONData containing the array of objects on the other.

In fact my code is crashing when I try to create a dictionary that contains an NSData object.

 NSArray <Offers *> *offers = [self getOffers];

 NSError *error;
        NSMutableArray<NSDictionary*> *jsonOffers = [NSMutableArray array];

        for (Offers* offer in offers) {

            [jsonOffers addObject:[offer toJSON]];
        }
        NSData * OffersJSONData = [NSJSONSerialization dataWithJSONObject:jsonOffers
                                                                 options:kNilOptions
                                                                   error:&error];   
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:50];

        [dict setObject:authtoken forKey:@"token"];
         [dict setObject:OffersJSONData forKey: @"offersobject"];
     //FOLLOWING LINE CRASHES   
        NSDictionary *jsonDictionary = [NSDictionary dictionaryWithDictionary:dict]; 

        NSData *data = [NSJSONSerialization dataWithJSONObject:jsonDictionary
                                                       options:0 error:&error];   
         [self postToServer:data];

Can anyone suggest a way to create a JSON object that can contain both a string and an NSData structure at the same time?

Thanks in advance for any suggestions.

user6631314
  • 1,751
  • 1
  • 13
  • 44
  • Add a method to `Offers` that convert it into a `NSDictionary`, with only objects of kind NSDictionary, NSArray, NSString, NSData (the mutable corresponding are allowed). – Larme Jul 07 '18 at 20:23
  • @Larme See their [previous question](https://stackoverflow.com/questions/51224184/ios-objective-c-convert-nsarray-of-custom-objects-to-json). They already have code to convert their custom class to JSON. – rmaddy Jul 07 '18 at 21:22
  • Actually, code I got from previous question -(NSDictionary*) toJSON { return @{@"value":self.offer}; is returning empty dictionary. Larme can you suggest how to get a valid dictionary from an NSManagedObject Offers with a property @property (nonatomic, retain) NSString * offer; ? – user6631314 Jul 08 '18 at 16:30

0 Answers0