该行动无法完成.(可可错误:3840.)

dhe*_*n79 8 json web-services objective-c nsjsonserialization ios6

我试图解析一个ios 6应用程序的JSON,但似乎无法让它工作.我已经搜索了大量的论坛,但没有找到一个有效的解决方案,我理解足以实现,或者适用.

如果有一个我错过了,我道歉.

首先,我有一个测试WebService,据我所知,返回有效的JSON

http://thetrouthunter.com/SVLocationsAPI.php

其次,这是我的Objective-C代码:

+ (NSDictionary *)connectToService:(NSString *)query
{
    NSError *error = nil;

    query = [NSString stringWithFormat:@"%@&format=json&nojsoncallback=1", query];
    query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];

    NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil;

    NSLog(@"locations: %@", results);

    if (error)
        NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);

    return results;
}

+ (NSArray *)userLocation {
    NSString *request = [NSString stringWithFormat:@"http://thetrouthunter.com/SVLocationsAPI.php"];
    return [[self connectToService:request] valueForKeyPath:@"locations.location"];
}
Run Code Online (Sandbox Code Playgroud)

ls NSLog函数打印出错误:"操作无法完成.(可可错误:3840.)"

我无法弄清楚为什么会这样.我尝试过各种各样的事情.

abe*_*ina 4

您要添加%@&format=json&nojsoncallback=1到 中的 URL connectToService:,新的 URL 会生成一个网页,而不是您期望的 JSON(即http://thetrouthunter.com/SVLocationsAPI.php&format=json&nojsoncallback=1)。

记录 HTTP 请求的实际结果可能很有用,以便您可以对其进行调试,直到获得 JSON(即在调用序列化函数之前)。