简体   繁体   中英

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

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

进行了上述更改,只是删除了TagMediaModel中的星形。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM