I am trying to get JSON data from this API, but I get error:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x8b5aa20 {NSDebugDescription=Invalid value around character 0.}
Here is the code:
NSString *post = [NSString stringWithFormat:@"http://api.reittiopas.fi/hsl/1_1_2/?request=reverse_geocode&user=********&pass=********&format=txt&coordinate=2548196,6678528"];
NSError *error = nil;
NSString* newStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:post] encoding:NSUTF8StringEncoding error: &error];
NSData *jsonDataString = [newStr dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", jsonDataString.description);
NSMutableDictionary *allResults = [NSJSONSerialization
JSONObjectWithData:jsonDataString
options:NSJSONReadingAllowFragments
error:&error];
if(!error){
NSLog(@"%@", allResults.description);
}
else{
NSLog(@"%@", error.description);
}
Can anyone tell me why I am getting this error. The code is working with other web's JSON data.
Your API does not return JSON at all, but pretty printed PHP array:
Edit: On the PHP manual one community member created the reverse part to print_r
(which is used in PHP to create the data you got).
http://www.php.net/manual/en/function.print-r.php#93529
You could translate this function into Objective-C
EDIT II: I created a class that addresses your challenge. You can get the current branch here on GitHub
Pretty printed array:
Array
(
[0] => Array
(
[locType] => address
[locTypeId] => 900
[name] => Purotie 8, Helsinki
[matchedName] =>
[lang] => fi
[city] => Helsinki
[coords] => 2548220,6678497
[distance] => 39.2045915678253
[details] => Array
(
[houseNumber] => 8
)
)
)
JSON:
[{"locType": "address", "locTypeId": 900, "name": "Purotie 8, Helsinki", "matchedName": "", "lang": "fi", "city": "Helsinki", "coords": "2548220,6678497", "distance": "39.2045915678253", "details": {"houseNumber": 8}}]
Pretty printed JSON:
[
{
"locType": "address",
"locTypeId": 900,
"name": "Purotie 8, Helsinki",
"matchedName": "",
"lang": "fi",
"city": "Helsinki",
"coords": "2548220,6678497",
"distance": "39.2045915678253",
"details": {
"houseNumber": 8
}
}
]