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);
相關文章
相關標籤/搜索