String转Json,数据格式没问题,始终报String cannot be converted to JSONObject的解决办法
2016-07-07 13:29
597 查看
服务器端返回的数据如下:
{"status":1,"msg":"","data":{"companyname":"","resCount":4,"res":[{"id":"120","sp":"\u56db\u5ddd\u7701","sc":"\u6210\u90fd\u5e02","ep":"\u5c71\u897f\u7701","ec":"\u592a\u539f\u5e02","product_type":"\u666e\u8d27","pack_type":"\u7bb1\u88c5","product_weight":"4.0","product_volume":"23.0","deadline":"4\u5929\u540e\u8fc7\u671f","distance":"\u8ddd\u79bb10.0\u516c\u91cc"},{"id":"119","sp":"\u56db\u5ddd\u7701","sc":"\u6210\u90fd\u5e02","ep":"\u5317\u4eac","ec":"\u5317\u4eac\u5e02","product_type":"\u666e\u8d27","pack_type":"\u7bb1\u88c5","product_weight":"21.0","product_volume":"21.0","deadline":"4\u5929\u540e\u8fc7\u671f","distance":"\u8ddd\u79bb11.4\u516c\u91cc"},{"id":"115","sp":"\u56db\u5ddd\u7701","sc":"\u6210\u90fd\u5e02","ep":"\u4e0a\u6d77","ec":"\u4e0a\u6d77\u5e02","product_type":"\u666e\u8d27","pack_type":"\u6563\u4ef6","product_weight":"3.0","product_volume":"30.0","deadline":"53\u5929\u540e\u8fc7\u671f","distance":"\u8ddd\u79bb20.8\u516c\u91cc"},{"id":"110","sp":"\u56db\u5ddd\u7701","sc":"\u6210\u90fd\u5e02","ep":"\u798f\u5efa\u7701","ec":"\u53a6\u95e8\u5e02","product_type":"\u666e\u8d27","pack_type":"\u7bb1\u88c5","product_weight":"13.0","product_volume":"12.0","deadline":"50\u5929\u540e\u8fc7\u671f","distance":"\u8ddd\u79bb28.0\u516c\u91cc"}],"page":1}}
绝对是一个正确的JSONObject格式,但 new JSONObject(res)的时候总是报Value of type java.lang.String cannot be converted to JSONObject。后来发现是UTF-8的BOM头问题,加上以下代码后正常:
if(res.startsWith("\ufeff")){
res = res.substring(1);
}在确认JSON数据格式没有问题的前提下,100%是因为UTF-8的BOM头。
{"status":1,"msg":"","data":{"companyname":"","resCount":4,"res":[{"id":"120","sp":"\u56db\u5ddd\u7701","sc":"\u6210\u90fd\u5e02","ep":"\u5c71\u897f\u7701","ec":"\u592a\u539f\u5e02","product_type":"\u666e\u8d27","pack_type":"\u7bb1\u88c5","product_weight":"4.0","product_volume":"23.0","deadline":"4\u5929\u540e\u8fc7\u671f","distance":"\u8ddd\u79bb10.0\u516c\u91cc"},{"id":"119","sp":"\u56db\u5ddd\u7701","sc":"\u6210\u90fd\u5e02","ep":"\u5317\u4eac","ec":"\u5317\u4eac\u5e02","product_type":"\u666e\u8d27","pack_type":"\u7bb1\u88c5","product_weight":"21.0","product_volume":"21.0","deadline":"4\u5929\u540e\u8fc7\u671f","distance":"\u8ddd\u79bb11.4\u516c\u91cc"},{"id":"115","sp":"\u56db\u5ddd\u7701","sc":"\u6210\u90fd\u5e02","ep":"\u4e0a\u6d77","ec":"\u4e0a\u6d77\u5e02","product_type":"\u666e\u8d27","pack_type":"\u6563\u4ef6","product_weight":"3.0","product_volume":"30.0","deadline":"53\u5929\u540e\u8fc7\u671f","distance":"\u8ddd\u79bb20.8\u516c\u91cc"},{"id":"110","sp":"\u56db\u5ddd\u7701","sc":"\u6210\u90fd\u5e02","ep":"\u798f\u5efa\u7701","ec":"\u53a6\u95e8\u5e02","product_type":"\u666e\u8d27","pack_type":"\u7bb1\u88c5","product_weight":"13.0","product_volume":"12.0","deadline":"50\u5929\u540e\u8fc7\u671f","distance":"\u8ddd\u79bb28.0\u516c\u91cc"}],"page":1}}
绝对是一个正确的JSONObject格式,但 new JSONObject(res)的时候总是报Value of type java.lang.String cannot be converted to JSONObject。后来发现是UTF-8的BOM头问题,加上以下代码后正常:
if(res.startsWith("\ufeff")){
res = res.substring(1);
}在确认JSON数据格式没有问题的前提下,100%是因为UTF-8的BOM头。
相关文章推荐
- Object和Function
- objective-C 编程全解-第15章 消息发送模式 下 NSUndoManager 撤销管理器
- objective-C 编程全解-第15章 消息发送模式 中
- Objective-C 编程全解-第15章 消息发送模式 上
- 判断字符串解析是JsonObject或者JsonArray
- iOS学习(十一)Objective-C 自动释放池
- 论文笔记 | Training Region-based Object Detectors with Online Hard Example Mining
- iOS学习(十)Objective-C 内存管理规则
- Objective-C-UI控件学习之下拉刷新
- iOS学习(九)Objective-C self和super
- 添加IObjectSafety接口使MFC写的OCX可信
- Object与RTTI
- JAVA源码分析之---Object类(一)---registerNatives,getClass方法的使用
- Objective-C 编程之道 iOS设计模式解析--第22章 代理
- 李洪强iOS开发之【Objective-C】07-自定义构造方法和description方法
- [精通Objective-C]运行时系统
- java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
- 对象属性的遍历
- Objective-C语法之字符串NSString
- selenium+java+testNG+pageObject深入理解自动化测试框架