CURestKit 1.0.4

CURestKit 1.0.4

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

未声明 维护。



CURestKit 1.0.4

  • 作者:
  • curer

CURestkit

ARC 支持

安装

platform :ios, '5.0'
pod 'CURestKit'

描述

灵感来源于 Restkit。

轻量级的 RESTful 服务器客户端。

我想让它用法简单,因为我不喜欢 coreData 以及那么多的类 : )。

JSON 映射类示例

JSON 文件

JSON:

{
    "user_h":
    {
        "name":"curer"
    },
    "text":"hello world come on curer"
}

模型类

@interface Status : NSObject

@property (nonatomic, retain) NSString *text;
@property (nonatomic, retain) User *user;
@property (nonatomic, retain) NSString *statusUsername;

@end

@interface User : NSObject

@property (nonatomic, retain) NSString *username;

@end

如何使用映射器

CUJSONMapper *statusMapper = [[[CUJSONMapper alloc] init] autorelease];
NSDictionary *statusMapping = @{@"user_h.name": @"statusUsername", @"text":@"text"};
[statusMapper registerClass:[Status class] andMappingDescription:statusMapping];

CUJSONMapper *userMapper = [[[CUJSONMapper alloc] init] autorelease];
NSDictionary *userMapping = @{@"name": @"username"};
[userMapper registerClass:[User class] andMappingDescription:userMapping];

[statusMapper addRelationship:userMapper
                 withJSONName:@"user_h"
                 atPropername:@"user"];

NSDictionary *statusDic = @{
                            @"user_h": @{
                                    @"name": @"curer"
                                    },
                            @"text": @"hello world come on curer"
                            };

Status *status = [statusMapper objectFromJSONDictionary:statusDic];

创建请求并解析 JSON 响应的示例

- (void)parseJSONFileDic
{
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"json1"
                                                                                  ofType:@"json"]];

    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:data
                                                    options:kNilOptions
                                                      error:&error];

    CUJSONMapper *mapper = [Status getObjectMapping];
    NSDictionary *statusDic = [jsonObject[@"statuses"] lastObject];

    Status *status = [mapper objectFromJSONDictionary:statusDic];

    NSLog(@"%@",status);
}

- (void)parseJSONFileArray
{
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"json1"
                                                                                  ofType:@"json"]];

    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:data
                                                    options:kNilOptions
                                                      error:&error];

    CUJSONMapper *mapper = [Status getObjectMapping];

    NSArray *list = [mapper objectfromJSONArray:jsonObject[@"statuses"]];

    NSLog(@"%@", list);
}

- (void)fetchList
{
    CUObjectManager *manager = [[CUObjectManager alloc] init];
    manager.baseURLString = @"https://api.weibo.com/2/";

    [manager registerMapper:[Status getObjectMapping]
               atServerPath:@"statuses/public_timeline.json"
                andJSONPath:@"statuses"];

    ASIHTTPRequest *request = 
    [manager getObjectsRequestAtPath:@"statuses/public_timeline.json"
                          parameters:@{
                                @"access_token" : SINA_TOKEN
                            }
                             success:^(ASIHTTPRequest *ASIRequest, NSArray *objects) {
                                 NSLog(@"%@", objects);
                             } error:^(ASIHTTPRequest *ASIRequest, NSString *errorMsg) {

                             }];

    [request startSynchronous];
}