JSONJoy 是一个为 iOS 和 Mac OSX 设计的小型库,它使将 JSON 转换和映射到你的对象变得简单快乐。
更多关于它的信息,请查看这里:http://vluxe.io/json-parsing.html
所以这是一个我们想要解析的 JSON 实体
{
"id" : 1
"first_name": "John",
"last_name": "Smith",
"age": 25,
"address": {
"id": 1
"street_address": "21 2nd Street",
"city": "New York",
"state": "NY",
"postal_code": 10021
}
}
这是我们想要将其转换成的 NSObjects
#import "Address.h"
@interface User : NSObject
@property(nonatomic,strong)NSNumber *objID;
@property(nonatomic,copy)NSString *firstName;
@property(nonatomic,copy)NSString *lastName;
@property(nonatomic,strong)NSNumber *age;
@property(nonatomic,strong)Address *address;
@end
@interface Address : NSObject
@property(nonatomic,strong)NSNumber *objID;
@property(nonatomic,copy)NSString *streetAddress;
@property(nonatomic,copy)NSString *city;
@property(nonatomic,copy)NSString *state;
@property(nonatomic,strong)NSNumber *postalCode;
@end
以下是一段容易被出错的样板代码
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSDictionary* response = responseObject;
User *john = [[User alloc] init];
john.objID = response[@"id"];
john.firstName = response[@"first_name"];
john.lastName = response[@"last_name"];
john.age = response[@"age"];
//now for the address
john.address = [[Address alloc] init];
NSDictionary* address = response[@"address"];
john.address.objID = address[@"id"];
john.address.streetAddress = address[@"street_address"];
john.address.city = address[@"city"];
john.address.state = address[@"state"];
john.address.postalCode = address[@"postal_code"];
//finally!, now do work with object
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
像这样Joyify起来:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSError *error = nil;
JSONJoy *joy = [JSONJoy JSONJoyWithClass:[User class]];
User *john = [joy process:responseObject error:&error];
//do work with object
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
在 NSObject 上还有一个类别,让你能够做到这样一行代码
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
User *john = [User objectWithJoy:responseObject];
//do work with object
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
需要添加一些自定义解析?没有问题,只需像下面的示例一样实现这个方法
+(JSONJoy*)jsonMapper
{
JSONJoy* mapper = [[JSONJoy alloc] initWithClass:[self class]];
[mapper addArrayClassMap:@"photos" class:[Photo class]];
return mapper;
}
所有类别方法都会调用这个方法,因此你的对象可以正确解析。
JSONJoy 通过将属性名映射到 JSON 值名来工作。它还支持将标准 Rails 蛇形案 JSON 名称转换为标准驼峰式 Objective-C 属性名。例如:
@property(nonatomic,copy)NSString *firstName;
支持以下格式的 JSON 值
"first_name": "John"
"firstName": "John"
安装 JSONJoy 的推荐方法是通过 CocoaPods 包管理器,因为它提供灵活的依赖关系管理,并且安装简单。
通过 CocoaPods
如果尚未安装,请安装 CocoaPods
$ [sudo] gem install cocoapods
$ pod setup
切换到 Xcode 项目的目录,创建并编辑你的 Podfile,并添加 JSONJoy
$ cd /path/to/MyProject
$ touch Podfile
$ edit Podfile
platform :ios, '5.0'
# Or platform :osx, '10.8'
pod 'JSONJoy'
将 JSONJoy 安装到你的项目中
$ pod install
从 .xcworkspace 文件(不是常规的项目文件)在 Xcode 中打开你的项目
通过 git 只需将 JSONJoy 添加为 git 子模块
JSONJoy 至少需要 iOS 5/Mac OSX 10.8 或更高版本。
JSONJoy 与 Apache 许可证他人共享。