NXJsonKit 0.4.1

NXJsonKit 0.4.1

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最新版本2017年7月

NXDrawKit 维护。



NXJsonKit 0.4.1

  • nicejinux

NXJsonKit 可轻松将 JSON 字典值设置到对象类型值或用户定义的数据模型。

为什么创建它?

我在寻找一个简单的 JSON 映射器用于项目,并在 GitHub 上找到了许多 JSON 映射的开源项目。但是那些不容易定制,并且有许多功能。
我们只需要一个简单的、容易使用的 JSON 映射器。

特性

  1. 自动将值设置到从 JSON 数据来的 Model 类。
  2. NSArray 元素添加映射条件。
  3. 重写 Model 属性 的键。
  4. 自动将 NSNumber 类型转换为原始类型 (NSInteger, CGFloat, enum...)。
  5. 将字符串类型值转换为 enum
  6. 将字符串类型日期转换为 NSDate
  7. 检查不应该为 nil 的值。

安装

手动

  1. 下载并将 /NXJsonKit 文件夹拖到您的项目中。
  2. 祝贺您!

用法

JSON 数据

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

从 JSON 获取对象

- (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;
}

逻辑

  1. 从指定的类获取所有属性。
  2. 检查自定义映射条件。
  3. 通过属性名或自定义映射条件获取数据。
  4. 如果将 <NXNotNull> 设置为想要检查的属性,如果 JSON 值为 nil,将调用 propertyWillSetNil:propertyClass: 方法。
  5. 如果数据类是集合类 (NSArray, NSDictionary) 或用户定义的类,则分配新的 NXJsonKit 并递归调用。
  6. 将值设置到 Model。

作者

这是 Jinwook Jeon
我在韩国担任 iOS 开发者。
我在等待您的评论、建议、修复等您想说的任何内容。
请随时联系我。

MIT许可证

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.