栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

NSJSONSerialization-无法将数据转换为字符串

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

NSJSONSerialization-无法将数据转换为字符串

大都会办公室数据点以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]);}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/413697.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号