APJSONMapping 2.0.1

APJSONMapping 2.0.1

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2016年1月

Alexander Perechnev 维护。



  • By Alex Krzyżanowski

一个Objective-C类扩展,您可以使用它轻松地将您的对象映射到JSON字符串以及从JSON解析回对象。

安装

获取 APJSONMapping 的一种最简单的方法是通过 CocoaPods 安装它

target 'MyApp' do
  pod 'APJSONMapping', '~> 1.0'
end

当框架被安装后,只需将其导入,以向现有类添加适当的功能

@import APJSONMapping;

@interface MyCustomClass : NSObject
// ...
@end

使用示例

为了让您的对象能够被映射到(并从)JSON,您必须描述它的映射规则

@import Foundation;
@import APJSONMapping;

//
// Here is interface
@interface MyCustomClass : NSObject

@property (nonatomic, strong) NSNumber *someNumber;
@property (nonatomic, strong) NSString *someString;

+ (Class)someArrayOfRelatingObjectsType;
@end

//
// And here is implementation
@implementation MyCustomClass

+ (NSMutableDictionary *)ap_objectMapping {
  NSMutableDictionary * mapping = [super ap_objectMapping];
  if (mapping) {
    NSDictionary * objectMapping = @{ @"someNumber": @"some_number",
                                      @"someString": @"some_string"};
    [mapping addEntriesFromDictionary:objectMapping];
  }
  return mapping;
}

@end

由于您已经描述了映射,现在您可以将对象映射到JSON并解析回来

MyCustomClass *myObject = [[MyCustomClass alloc] init];
myObject.someNumber = @112;
myObject.someString = @"testString";

NSString *json = [myObject ap_mapToJSONString]; // { "some_number": 112, "some_string": "testString" }
MyCustomClass *anotherObject = [[MyCustomClass alloc] initWithJSONString_ap:json];

代码覆盖率