NXJsonKit 可轻松将 JSON 字典值设置到对象类型值或用户定义的数据模型。
我在寻找一个简单的 JSON 映射器用于项目,并在 GitHub 上找到了许多 JSON 映射的开源项目。但是那些不容易定制,并且有许多功能。
我们只需要一个简单的、容易使用的 JSON 映射器。
NSArray
元素添加映射条件。NSNumber
类型转换为原始类型 (NSInteger
, CGFloat
, enum
...)。enum
。NSDate
。nil
的值。/NXJsonKit
文件夹拖到您的项目中。 {
"user_name":"Nicejinux",
"age":39,
"numberOfFriends":3,
"hasGirlFriend":false,
"height":178.5,
"birthday":"20000101"
"job":"DEVELOPER"
"pets":[
{
"kind":"dog",
"name":"doggy",
"age":"2 years",
}, {
"kind":"cat",
"name":"kitty",
"age":"1 year"
}
]
}
// People.h
// enum type
typedef NS_ENUM (NSInteger, JobType) {
JobTypeNone = 0,
JobTypeDoctor,
JobTypeDeveloper,
JobTypeDesigner,
};
// People Model
@interface People : NSObject
@property (nonatomic, strong) NSString <NXNotNull> *name;
@property (nonatomic, strong) NSNumber *age;
@property (nonatomic, strong) NSArray <Pet *> *pets;
@property (nonatomic, strong) NSDate *birthday;
@property (nonatomic, assign) JobType jobType;
@property (nonatomic, assign) NSInteger numberOfFriends;
@property (nonatomic, assign) BOOL hasGirlFriend;
@property (nonatomic, assign) CGFloat height;
@end
// Pet Model
@interface Pet : NSObject
@property (nonatomic, strong) NSString *kind;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSNumber *age;
@end
// People.m
// optional method for NXNotNull protocol
- (void)propertyWillSetNil:(NSString *)propertyName propertyClass:(Class)propertyClass
{
// You can assert or do something else here.
NSLog(@"%@ (%@) property should not be nil", propertyName, propertyClass);
}
- (People *)mapJsonToPeopleModelWithData:(NSDictionary *)dic
{
// create mapper for each mappings.
NXMapper *mapper = [[NXMapper alloc] init];
// add array mapping with element class
// pets (NSArray) element will map as a Pet class in the People class
NXArrayMapping *arrayMapping = [NXArrayMapping mapForArrayItemClass:Pet.class itemKey:@"pets" onClass:People.class];
[mapper addArrayMapping:arrayMapping];
// add object mapping with field name (different object name)
// "user_name" which is from Json data will map to "name" property in the People class
NXObjectMapping *objectMapping = [NXObjectMapping mapForJsonKey:@"user_name" toModelKey:@"name" onClass:People.class];
[mapper addObjectMapping:objectMapping];
// "job" which is from Json data will map to "jobType" in the People class
objectMapping = [NXObjectMapping mapForJsonKey:@"job" toModelKey:@"jobType" onClass:People.class];
[mapper addObjectMapping:objectMapping];
// add date mapping with formatter
// birthday will map as a NSDate with formatter (yyyyMMdd)
NXDateMapping *dateMapping = [NXDateMapping mapForDateKey:@"birthday" formatter:@"yyyyMMdd" onClass:People.class];
[mapper addDateMapping:dateMapping];
// add enum mapping with enum type list
// "jobType" which is from Json data "job" will map as JobType in the People class
NXEnumMapping *enumMapping = [NXEnumMapping mapForEnumKey:@"jobType" enumTypeList:@[@"NONE", @"DOCTOR", @"DEVELOPER", @"DESIGNER"] onClass:People.class];
[mapper addEnumMapping:enumMapping];
// initialize jsonkit with Json data and Mapper
NXJsonKit *jsonKit = [[NXJsonKit alloc] initWithJsonData:dic mapper:mapper];
// get mapped object that you specified class
People *people = [jsonKit mappedObjectForClass:[People class]];
return people;
}
<NXNotNull>
设置为想要检查的属性,如果 JSON 值为 nil,将调用 propertyWillSetNil:propertyClass:
方法。NSArray
, NSDictionary
) 或用户定义的类,则分配新的 NXJsonKit
并递归调用。这是 Jinwook Jeon
我在韩国担任 iOS 开发者。
我在等待您的评论、建议、修复等您想说的任何内容。
请随时联系我。
Copyright (c) 2017 Jinwook Jeon. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.