String转Json,数据格式没问题,始终报String cannot be converted to JSONObject的解决办法

服务器端返回的数据如下:

{"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头。

猜你喜欢

转载自blog.csdn.net/eclothy/article/details/51850491
今日推荐