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.