我在NS
JSONSerialization中遇到问题,从Met Office Datapoint API中读取JSON.
我收到以下错误
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unable to convert data to string around character 58208.
我已经检查,认为这是根据人物位置的违规行
{"id":"353556","latitude":"57.1893","longitude":"-5.0929","name":"Sóil Chaorainn"}
根据我尝试的几个验证器,JSON本身似乎是有效的,我也期望它也来自一个大型组织,如Met Office.
NSJSONSerialization不应该像“ó”这样的字符正常工作吗?
如果不是如何改变编码类型来处理这个?
提前谢谢了
解决方法
Met Office数据点发送回ISO-8859-1中的数据,该数据不是NSJSONSerialization支持的数据格式之一.
为了使其工作,首先使用NSISOLatin1StringEncoding从URL内容创建一个字符串,然后创建要在NSJF中使用NSDataF8编码的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 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]); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。