Android 2.3以下Json解析错误(bom)
问题
2.3及以下的系统版本在解析json数据时发现报 10-12 02:36:35.964: W/System.err(323): org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
错误。
原因
当UTF-8编码带有bom头时,json无法解析
解决
方法一
服务器返回json时,编码utf-8不要带bom
方法二
对比android4.0的json包
4.0 代码
public JSONTokener(String in) {
// consume an optional byte order mark (BOM) if it exists
if (in != null && in.startsWith("ufeff")) {
in = in.substring(1);
}
this.in = in;
}
而2.3的代码
public JSONTokener(String in) {
is.in = in;
}
问题很简单了在解析前将String过滤一下就ok。