Reputation: 874
I have this object:
@interface EmailToSend : NSObject
@property (copy, nonatomic) NSString *Subject;
@property (copy, nonatomic) NSString *Body;
@property (strong, nonatomic) NSArray *Cc;
@property (strong, nonatomic) NSArray *Bcc;
@property (strong, nonatomic) NSArray *To;
@property (strong, nonatomic) EmailAddress *From;
@property (copy, nonatomic) NSString *Username;
@property (copy, nonatomic) NSString *Password;
@end
Bcc, To, From as Array EmailAdress
@interface EmailAddress : NSObject
@property (nonatomic, assign) int Id;
@property (copy, nonatomic) NSString *address;
@property (copy, nonatomic) NSString *displayName;
@end
I use the JSON framework in iOS to parse object EmailToSend
:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:emailToSend options:NSJSONWritingPrettyPrinted error:&writeError];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
When I run the project, one error appears:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'Invalid type in JSON write
(EmailToSend)
How to fix it?
Upvotes: 1
Views: 794
Reputation: 13864
EmailToSend
is a class type that you use in your application. However, the built-in JSON serializer in Cocoa can only work with simple types such as NSString
NSArray
etc. You will have to make you From
property a string if you want it to work.
And as pointed out by Wain in his comment, the root element also needs to be an NSArray
or NSDictionary
.
Upvotes: 3