前几天在IOS开发时发现一个JSON解析奇葩问题

Alan_Chen_2011 2014-01-20 03:15:52
前几天在IOS开发时发现一个JSON解析奇葩问题,会出现一定概率的错误,如下:
//出现BUG的条件必须是两位数,且带两位小数,类型还必须是float
//两位数:十位必须是7、8、9;个位数随意
//两位小数:个位数随意;十位数必须是0
NSString *jsonStr = @"{\"71.40\":71.40, \"97.40\":97.40, \"80.40\":80.40, \"188.40\":188.40}";
NSLog(@"json:%@", jsonStr);
NSData *jsonData_ = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonParsingError_ = nil;
NSDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:[NSJSONSerialization JSONObjectWithData:jsonData_ options:0 error:&jsonParsingError_]];
NSLog(@"dic:%@", dic);
/*结果:dic:{
"188.40" = "188.4";
"71.40" = "71.40000000000001";
"80.40" = "80.40000000000001";
"97.30" = "97.3";
}*/


各位大神可是试试,初步怀疑是系统API内部处理问题。
...全文
3715 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
SadnessJoin 2016-07-07
  • 打赏
  • 举报
回复
希望帮到你们 http://www.jianshu.com/p/83d4bc28cc7c
Capricorn_Tong 2015-12-29
  • 打赏
  • 举报
回复
楼主 怎么解决的 急急急
CC的糖豆 2015-05-13
  • 打赏
  • 举报
回复
正常啊 数据格式错误。你的JSon格式里面的值是float,你解析之后作为对象放到字典里面,被转成NSString,当然有误差了。不用纠结。
端_木 2014-02-14
  • 打赏
  • 举报
回复
//解析json数据,使用系统方法 JSONObjectWithData: options: error: NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
SiamPig 2014-02-10
  • 打赏
  • 举报
回复
晕死,看一下API吧,是你参数传递错误额 /* Create a Foundation object from JSON data. Set the NSJSONReadingAllowFragments option if the parser should allow top-level objects that are not an NSArray or NSDictionary. Setting the NSJSONReadingMutableContainers option will make the parser generate mutable NSArrays and NSDictionaries. Setting the NSJSONReadingMutableLeaves option will make the parser generate mutable NSString objects. If an error occurs during the parse, then the error parameter will be set and the result will be nil. The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. The data may or may not have a BOM. The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8. */ + (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error; 说的很明白的阿,当参数不是一个数组,或者字典的时候,opt才会传0阿。。。。
cchvsgame 2014-01-20
  • 打赏
  • 举报
回复
数据格式错误吧

29,031

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧