I am trying to parse JSON for an ios 6 application, but it can't seem to get it to work. I went through a lot of forums, but did not find a solution that works, which I understand enough to implement, or is it applicable.
I apologize if there is one that I missed.
First I have a test WebService which, as far as I can tell, returns a valid JSON
http://thetrouthunter.com/SVLocationsAPI.php
Secondly, here is my Objective-C code:
+ (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"]; }
The ls NSLog function displays an error message: "The operation could not be completed. (Cocoa error: 3840.)"
I canβt understand why this is so. I tried all kinds of things.
json objective-c web-services ios6 nsjsonserialization
dherrin79
source share