Mat*_*ice 9 cocoa-touch json web-services objective-c ios
我遇到了从Met Office Datapoint API读取JSON的NSJSONSerialization的问题.
我收到以下错误
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unable to convert data to string around character 58208.
Run Code Online (Sandbox Code Playgroud)
我已根据角色位置检查并认为这是违规行
{"id":"353556","latitude":"57.1893","longitude":"-5.0929","name":"Sóil Chaorainn"}
Run Code Online (Sandbox Code Playgroud)
根据我尝试的几个验证器,JSON本身似乎是有效的,我希望它也来自像Met Office这样的大型组织.
NSJSONSerialization不应该与'ó'这样的字符一起使用吗?
如果不是我如何改变编码类型来处理这个?
提前谢谢了
Eri*_*net 21
Met Office Datapoint发回ISO-8859-1中的数据,这不是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 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]);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9311 次 |
| 最近记录: |