3
votes

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.

2
Use [NSData dataWithContentsOfUrl:...]k06a
But the response is not JSON.Desdenova
See my second edit, I created a GitHub repo with a class that addresses your challenge: github.com/julian-weinert/JWPrettyPrintedSerializationJulian F. Weinert
To help you more easily, you could just change format=txt to format=json in your API address. I tested it, it'll work fineJulian F. Weinert

2 Answers

4
votes

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
        }
    }
]
3
votes

The API is not returning well-formatted JSON, so it cannot be parsed.

You can use a site like http://jsonlint.com/ to confirm that the API's output is improperly formatted and therefore can't be parsed by NSJSONSerialization