9

I'm having a problem with NSJSONSerialization reading JSON from the Met Office Datapoint API.

I get the following error

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unable to convert data to string around character 58208.

I have checked and think this is the offending line according to the character position

{"id":"353556","latitude":"57.1893","longitude":"-5.0929","name":"Sóil Chaorainn"}

The JSON itself appears to be valid according to a couple of Validators I tried, and I would expect it too be from a large organisation such as Met Office.

Shouldn't NSJSONSerialization work fine with characters such as 'ó'?

If not how do I go about changing the encoding type to deal with this?

Many Thanks in Advance

1
  • Try a different JSON package -- one that accepts an NSString -- so you can be sure about the conversion. Commented Jan 23, 2013 at 18:24

2 Answers 2

21

The Met Office Datapoint sends back data in ISO-8859-1 which isn't one of the supported data format for NSJSONSerialization.

To make it work create a string from the URL content at first with NSISOLatin1StringEncoding and then create the NSData you want to use in the NSJSONSerialization with a NSUTF8 encoding.

The following works to create the corresponding json object

NSError *error;
NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/json/sitelist?key=<YOUR_API_KEY"] encoding:NSISOLatin1StringEncoding error:&error];

NSData *metOfficeData = [string dataUsingEncoding:NSUTF8StringEncoding];

id jsonObject = [NSJSONSerialization JSONObjectWithData:metOfficeData options:kNilOptions error:&error];

if (error) {
    //Error handling
} else {
    //use your json object
    NSDictionary *locations = [jsonObject objectForKey:@"Locations"];
    NSArray *location = [locations objectForKey:@"Location"];
    NSLog(@"Received %d locations from the DataPoint", [location count]);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I'm using the following NSData* data = [NSData dataWithContentsOfURL: kMetOfficeAllSites]; From the following URL datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/json/… API> Then NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
Just made it work. In fact the encoding from the dataPoint is ISO-8859-1 (NSISOLatin1). you just need to get it in a String and create a UTF-8 NSData from it. I'm updating my answer above with it.
Many Thanks Eric for the solution. I guess this will be the best way to handle all JSON from Datapoint as its all likely to be ISO-8859-1. Can I ask how you found the encoding type?
You're welcome, it's not that elegant but it works. To find the encoding I used curl http://...... -o outputfile on the command line and then looked at the encoding of the file. There's nothing on the HTTP header of the datapoint (which for me is a bug)
Yeah, I might try contact them and ask about the encoding of the JSON and their HTTP header. But I don't have a great deal of knowledge in this area. Once again many thanks.
4

What is encoding for the JSON? JSON is supposed to UTF-8 but I have seen lousy APIs where they use ISO-8859-1. NSJSONSerialization only works UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE.

1 Comment

I'm not sure the best way to check the encoding, but i loaded the JSON feed in chrome and under Tools / Encoding it said Western(ISO-8859-1) so looks like i'm out of luck, can you re-encode?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.