abi*_*son 6 objective-c restkit afnetworking restkit-0.20
所以我刚刚切换到RestKit 0.2,我目前正在使用新的"HttpClient",它基本上是一个AFHTTPClient.我有这行代码:
RKObjectManager* objectManager = [RKObjectManager sharedManager];
NSDictionary* params = [[NSDictionary alloc] initWithObjectsAndKeys: login, @"username", password, @"password", nil];
[[objectManager HTTPClient]postPath:@"users/login/?format=json" parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
//reponseObject vs operation.response
NSLog(@"%@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"ERROR");
}];
Run Code Online (Sandbox Code Playgroud)
此POST调用以以下格式返回JSON响应:{"api_key":"....","username":"...."}.就如此容易.
在切换到0.2之前,我能够通过执行以下操作获取响应中的api_key键:
[[RKClient sharedClient] post:@"/users/login/?format=json" usingBlock:^(RKRequest *request)
{
request.onDidLoadResponse = ^(RKResponse *response)
{
id parsedResponse = [response parsedBody:NULL];
NSString *apiKey = [parsedResponse valueForKey:@"api_key"];
}
}.....];
Run Code Online (Sandbox Code Playgroud)
http://restkit.org/api/master/Classes/RKResponse.html
但是现在,我不能这样做,如果我在responseObject上做一个NSLog,我得到:
<7b227265 61736f6e 223a2022 41504920 4b657920 666f756e 64222c20 22617069 5f6b6579 223a2022 61356661 65323437 66336264 35316164 39396338 63393734 36386438 34636162 36306537 65386331 222c2022 73756363 65737322 3a207472 75657d>
奇怪的是,如果我这样做:
NSLog(@"%@", operation.responseString);
Run Code Online (Sandbox Code Playgroud)
我确实有JSON(在NSString中)出现.
所以有两个问题:
1)为什么打印responseObject显示我的HEX代码,而不是实际的JSON响应?
2)为什么我要执行operation.responseString它显示实际的响应对象?有没有办法在从JSON解析后获取ResponseObject中的实际数据?
谢谢!
Fel*_*lix 17
AFNetworking应该实例化一个AFJSONRequestOperation.可能它创建了一个基本的AFHTTPRequestOperation(检查[operation class]),导致NSData对象作为响应.
确保在AFHTTPClient子类的init方法中注册操作类(initWithBaseURL):
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[self setDefaultHeader:@"Accept" value:@"application/json"];
Run Code Online (Sandbox Code Playgroud)
您也可以尝试直接使用AFJSONRequestOperation,如下所示:
NSURLRequest *request = [[objectManager HTTPClient] requestWithMethod:@"POST" path:@"users/login/?format=json" parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"JSON: %@", JSON);
} failure:nil];
[[objectManager HTTPClient] enqueueHTTPRequestOperation:operation];
Run Code Online (Sandbox Code Playgroud)
Dan*_*ore 11
你所看到的,如果我没有弄错的话,是你调用成功块时给你的NSData的原始字节.
您发布的十六进制读取:
{"reason": "API Key found", "api_key": "a5fae247f3bd51ad99c8c97468d84cab60e7e8c1", "success": true}
第二个NSLog显示你想要的是原因是%@格式字符串调用你传递它的对象的描述(如果我错了,那么纠正我,那么),并且NSData可能知道它是一个字符串.
那么,关于如何获得JSON.这真的很简单.获得响应对象后,可以执行以下操作:
NSDictionary* jsonFromData = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
这是什么会为你做的是使用返回一个NSDictionary编码根对象的JSON,然后在字典中的每个值将是类型NSString,NSNumber,NSArray,NSDictionary,或NSNull.有关文档,请参阅NSJSONSserialization.
将NSJSONReadingMutableContainers使得字典和数组可变的.它只是我代码中的遗留物.
希望您使用的是iOS 5或更高版本,或者您需要找到另一种解析解决方案.
| 归档时间: |
|
| 查看次数: |
5604 次 |
| 最近记录: |