Value of type java.lang.String cannot be conver...

这个错误的缘由是JSON带了utf-8的BOM头,须要去掉就能够正常解析了 java

在android4.0开始,带有内部解析的,可是4.0如下就没有了 android

具体是源码是: json

4.0 this

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;
    }
4.0如下

public JSONTokener(String in) {
         this.in = in;
    }
看出来区别的了吧

只须要对你的json串进行这样一个处理就能够正常使用了 code

String jsonStr; // 须要解析json格式的字符串
if(jsonStr != null && jsonStr.startsWith("\ufeff"))
{
     jsonStr =  jsonStr.substring(1);
}
JSONObject json = new JSONObject(jsonStr);
相关文章
相关标签/搜索