Restkit 2 JSON示例

时间:2013-10-25 06:22:18

标签: ios restkit

我正在为Restkit 2寻找一个很好的教程。我所看到的每个地方,他们都在谈论对象映射。是否无法使用Restkit并获取JSON作为字符串,然后直接使用JSON。

1 个答案:

答案 0 :(得分:0)

AFNetworking做的工作,

可以使用cocoapads安装AFNetworking,如here

所示

使用AFNetworking的示例请求:

NSURL *url = [[NSURL alloc] initWithString:@"https://www.ez-point.com/search"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue:@"xxxxxxxxxxx" forHTTPHeaderField:@"Authorization" ];
[request setHTTPMethod:@"GET"];

NSMutableDictionary *jsonDic = [[NSMutableDictionary alloc]init];
[jsonDic setValue:@"UJO526" forKey:@"search_text"  ];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:NSJSONWritingPrettyPrinted error:nil];
[request setHTTPBody:jsonData];


[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
            NSArray *searchResults = JSON;
            if ([searchResults count] == 1){
                id result = [searchResults objectAtIndex:0];
                double latitude = [[result valueForKey:@"latitude"] doubleValue];
                double longitude = [[result valueForKey:@"longitude"] doubleValue];
                NSString *ezPoint = [result valueForKey:@"value"];
                NSString *tags = [result valueForKey:@"tags"];

                [self setAnnotation:latitude  ForLongitude:longitude withEZPoint:ezPoint WithTags:tags];
            }
        }
        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

        }
 ];
[operation start];