Shakti
Shakti

Reputation: 996

Serialize JSONModel - How to convert custom object to JSON in iOS?

How to serialize a custom object with JSONModel ?

@property(nonatomic,strong) NSString<Optional> * tag_post_type;
@property(nonatomic,strong) NSString<Optional> * tag_users_type;
@property(nonatomic,strong) NSArray<Optional> * tags;
@property(nonatomic,strong) NSMutableArray<TagMediaModel*>* tag_posts;
@property(nonatomic,strong) NSMutableArray<TagLocationModel*>* locations;

I try to create a JSON file out of my custom Object with the JSONModel framework for iOS. I get the Error:

EXCEPTION: Invalid type in JSON write (TagMediaModel)

When I call toJSONString Method, I got this issue.

[tagAutomaticModel toJSONString];

This is the model data:

 locations =     (
    "<TagLocationModel> \n   [id]: 780307344\n   [name]: Hotel Central Park, india\n</TagLocationModel>",
    "<TagLocationModel> \n   [id]: 463004401\n   [name]: Miraj Cinema new year\n</TagLocationModel>",
    "<TagLocationModel> \n   [id]: 246187965\n   [name]: Surya Treasure Island asia\n</TagLocationModel>",
);
"tag_posts" =     (
    "<TagMediaModel> \n   [media_code]: BS0tQeFhU_Z\n   [media_id]: 1492016420475981785\n   [media_url]: https://scontent.cdninstagram.com/t51.2885-15/e15/17881459_...\n   [media_type]: I\n</TagMediaModel>"
);

Upvotes: 0

Views: 334

Answers (2)

Balasubramanian
Balasubramanian

Reputation: 5510

The angle brackets after NSMutableArray contain a protocol. As @R.Mohan said, you need to remove * pointer and use without pointer.

  1. The protocol must be in place. So add @protocol tag_posts and @protocol locations at top in respective class.
  2. EXCEPTION: Invalid type in JSON write (TagMediaModel) I guess you are converting json to your custom class one or more times. Do it only once. It will resolve your problem.

Upvotes: 0

Mohan Ramanathan
Mohan Ramanathan

Reputation: 2209

@property(nonatomic,strong) NSMutableArray<TagMediaModel>* tag_posts;

made few change as above, just removed the star in TagMediaModel.

Upvotes: 1

Related Questions