MAC IOS JSON operation model to Json

Source: Internet
Author: User

在移动网络时代,json成为了主流的数据交换格式。如何能够方便快捷的创建、转化、传递json文件称为了开发者必备的技能。幸好,我们生活在开源时代,很多功能不需要我们重现造轮子。今天我推荐一款开源json处理库。项目地址:https://github.com/icanzilb/JSONModel我要实现class对象可以输出下列json:
其实直接编写也很容易,不过采用对象的方式后期维护十分麻烦。因而我把上述json抽象成了class。如下

JSContentsModel.h

```

Import JSONModel.hImport RFIConModel.h

@interface Jscontentsmodel:jsonmodel

@property (Strong, nonatomic) nsarray * images;
@end
```

继承了JSONModel后,JSContentsModel就可以自由的转化json了。其中的images就是对json中的images数组的抽象。下面,我将对数组内容进行抽象。

Rficonmodel
```

@protocol Rficonmodel @end

@interface Rficonmodel:jsonmodel
@property (nonatomic,strong) nsstring * filename;
@property (nonatomic) nsnumber * imagewidth;
@property (nonatomic) nsnumber * imageheight;
@property (nonatomic) nsstring * scale;
@property (nonatomic) NSString * comments;

@end
```

 RFIconModel对象是对images数组内容的抽象。这样就完成了对json文件的全部工作。怎么样简单又快捷吧。 最后测试代码如下:
    JSONModelArray * jImages=[[JSONModelArray alloc] initWithArray:images modelClass:[RFIConModel  class]];        JSContentsModel * oneModel=[[JSContentsModel alloc] init];    oneModel.images=images;    NSLog(@"%@",[oneModel toJSONString]);

MAC IOS JSON operation model to Json

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.