【发布时间】:2012-08-16 05:35:31
【问题描述】:
我创建了一个 servlet,它使用从 json 数据创建的字节数组响应 get 请求。我正在尝试在 iOS 中使用这些数据并使用 NSJSONSerialization 将其解析为 NSDictionary,但它失败并出现以下错误
Error Domain=NSCocoaErrorDomain Code=3840“操作无法完成。(Cocoa 错误 3840。)”(字符 11 周围对象的重复键。) UserInfo=0x6833200 {NSDebugDescription=字符 11 周围对象的重复键。 }
这是我的代码:
NSString *query = @"http://localhost:8888/url?method=retrieve";
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:query]];
NSError *error = nil;
NSString *stringData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"substring to index 255: %@", [stringData substringToIndex:255]);
NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:[stringData dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error] : nil;
NSLog(@"Response as Dictionary:\n%@", results.description);
if (error) {
NSLog(@"Error: %@", error);
}
stringData 的值为
{"APPEALS":{"APPEAL":{"AppealID":387423483,"LastEdit":"1 ...
响应为 Dictionary 返回 (null) 显然因为存在错误 我猜这与我的服务器将输出流中的数据作为 byte[] (java) 发送并且在 iOS 中接收时未正确格式化为 json 的事实有关,但这没有意义对我来说为什么它会在第 11 个字符“:”处失败 仅供参考,服务器是用 Java 编写在 Google App Engine 上的,localhost url 是本地开发服务器。 json 数据是使用 Jackson Generator 库创建的。谢谢!
【问题讨论】: