我刚开始使用restkit for ios。我有一个非常简单的 json 响应
{
“结果”: [
{
“用户名”:“5964”,
“名字”:“你的名字”
}
]
}
这是一个有效的 json(针对 jsonlint 测试)
但是,当我尝试将其与 RKClient 一起使用时,它不会将其视为 json。这是我的代码
-(void) request:(RKRequest *)request didLoadResponse:(RKResponse *)response{
if([request isGET]){
if([response isJSON]){
NSLog(@"JSON Response from Server %@", [response bodyAsString] );
}else{
NSLog(@"Non JSON Response from Server %@", [response bodyAsString] );
}
}
}
它总是记录“非 JSON 响应 ....”,但 json 是完全有效的。当我使用 RKObjectMapping 时也会发生同样的事情;错误将是“无法找到 MIME 类型文本/xml 的解析器。
非常感谢任何帮助。
谢谢
最佳答案
如果由于任何原因您无法更改服务器 header 并将 Mime 类型返回为 xml 或 json,您仍然可以添加要处理为 json 的 mime 类型“text/html”的映射。
只需添加:
[[RKParserRegistry sharedRegistry] setParserClass:[RKJSONParserJSONKit class] forMIMEType:@"text/html"];
并且不要忘记添加相关的头文件:
#import <RestKit/RKJSONParserJSONKit.h>
为我工作。
看看https://github.com/RestKit/RestKit/wiki/Using-a-Custom-MIME-Type
关于ios - Restkit 无法读取有效的 json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9781654/