Conversion between json strings and objective-c Models

Source: Internet
Author: User

In today's open openapi era, it is necessary to find a library that easily converts json strings into models in a familiar language. On the NET platform, we have Newtonsoft. if the Json library is used, what can we use on the ios platform? when developing the iphone, I found the jastor library. Now I will introduce the basic usage.

Suppose we have the following class:

<Foundation/Foundation. h> @ property (nonatomic, strong) NSNumber * @ property (nonatomic, strong) NSNumber * @ property (nonatomic, copy) NSString * <Foundation/Foundation. h> @ property (nonatomic, strong) NSNumber * @ property (nonatomic, strong) NSNumber * @ property (nonatomic, copy) NSString * @ property (nonatomic, strong) NSArray * + [DeviceEntityView Code

Note that we need to use NSNumber for packaging when defining the corresponding attribute as the basic type. The above example also shows that we can use arrays as an attribute, you only need to tell it what type the array is when it is implemented. The attribute name you define is followed by the _ class format. Note that this cannot be wrong.

When a service is called, the other party generally returns a json string. What we need to do is instantiate an NSDictionary based on this string, and then we can instantiate the corresponding model based on this NSDictionay, this is much easier than parsing the string directly. The Code is as follows:

 NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:,,,,, DeviceEntity *device = [[DeviceEntity alloc] initWithDictionary:dictionary];

We can verify that,

 NSLog( NSLog( NSLog(,[device.isopen intValue]);  

Will print out

 -- :: objc-grammar-learing[:f803] device -- :: objc-grammar-learing[:f803] device -- :: objc-grammar-learing[:f803] device

Check whether it is convenient. Of course, the above is just a very simple model. In general, models in real projects must be more complex than this, such as one-to-one and one-to-many models, there are examples available on the official website for reference.

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.