NSJSONSerialization - 无法将数据转换为字符串 [英] NSJSONSerialization - Unable to convert data to string

查看:294
本文介绍了NSJSONSerialization - 无法将数据转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用NSJSONSerialization从Met Office Datapoint API读取JSON时遇到问题。



我收到以下错误

 错误Domain = NSCocoaErrorDomain Code = 3840操作无法完成。(Cocoa错误3840.)(无法将数据转换为字符58208周围的字符串。

我已检查并认为这是根据字符位置的违规行

  {id:353556,latitude:57.1893,longitude: -  5.0929,name:sóilChaorainn } 

根据我尝试的两个验证器,JSON本身似乎有效,



NSJSONSerialization不应该使用'ó'等字符吗?



如果没有,我该如何改变编码类型来处理这个问题?



很感谢提前


$



为了使其工作,首先使用NSISOLatin1StringEncoding从URL内容创建一个字符串,然后使用NSUTF8编码创建要在NSJSONSerialization中使用的NSData。



以下工作创建相应的json对象

  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错误:&错误];

NSData * metOfficeData = [string dataUsingEncoding:NSUTF8StringEncoding];

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

if(error){
//错误处理
} else {
//使用你的json对象
NSDictionary * locations = [jsonObject objectForKey: @Locations];
NSArray * location = [locations objectForKey:@Location];
NSLog(@从DataPoint接收%d个位置,[位置计数]);
}


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

解决方案

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]);
}

这篇关于NSJSONSerialization - 无法将数据转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆