0

i've a little json encode problem :

i need to encode an object format JSON with SBJSON before send it to a php server At the moment this sample code work :

NSArray *arrayData = [NSArray arrayWithObjects:
                      user.id == nil ? [NSNumber numberWithInt:-1] : user.id,
                      ProfessionField.text, NameField.text, RPPSField.text, RPPSField.text,
                      NameField.text, SurnameField.text, StreetField.text,
                      TownField.text, CpField.text, MailField.text,
                      PhoneField.text, FaxField.text, MobileField.text,
                   //   [user.horaires JSONRepresentation],
                      nil];

NSArray *arrayKey = [NSArray arrayWithObjects:
                     @"id", @"spe", @"name", @"rpps", @"cip",
                     @"name", @"surname", @"rue",
                     @"ville", @"cp", @"mail", 
                     @"tel", @"fax", @"port", 
                    // @"horaires",
                     nil];

NSDictionary *dataBrut = [NSDictionary dictionaryWithObjects:arrayData forKeys:arrayKey];
NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:dataBrut forKey:@"data"];
NSString *jsonRequest = [jsonDict JSONRepresentation];

The problem is when i need to send the "user.horaires" (here in comment) Application CRASH at the JSON representation of this object.

this object is an Array of the following class :

@interface Horaire : NSObject
{ 
    BOOL morning;
}

@property (nonatomic, strong) NSNumber  *id;
@property (nonatomic, strong) NSString  *open;
@property (nonatomic, strong) NSString  *close;

Someone know how to succes to encode this ?

Damien Locque
  • 1,810
  • 2
  • 20
  • 42

1 Answers1

1

You shouldn't be including the JSON representation as a JSON item. JSON does not "escape" string data very well, so the embedded JSON (unless you separately "escape" it) will cause parsing to choke.

Instead you should place the dictionary or array that was used to produce the JSON representation (ie, "user.horaires" itself) in the location where you show the representation being produced and inserted. Then the entire structure will be JSON-encoded in one operation.

Ie:

NSArray *arrayData = [NSArray arrayWithObjects:
                  user.id == nil ? [NSNumber numberWithInt:-1] : user.id,
                  ProfessionField.text, NameField.text, RPPSField.text, RPPSField.text,
                  NameField.text, SurnameField.text, StreetField.text,
                  TownField.text, CpField.text, MailField.text,
                  PhoneField.text, FaxField.text, MobileField.text,
                  user.horaires,
                  nil];
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • i've try befor but it answer : JSONRepresentation failed. Error is: JSON serialisation not supported for Horaire IDK why cause my array of object is pretty simple – Damien Locque May 15 '12 at 16:06
  • 1
    Well, is that object an array or dictionary, or some other object that implements it's own JSONRepresentation method? – Hot Licks May 15 '12 at 16:08
  • oh ok i was dreaming :p i've implement the JSONRepresentation of this object and now it work fine, ty – Damien Locque May 15 '12 at 16:16