java中JSONException: java.lang.String cannot be converted to JSONObject
在Android系统中进行JSON解析时碰到以下问题:
org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject
使用Notepad 设置文件编码时,采用utf8编码,出现上面的问题。原因是文件中包含了utf8 bom头信息。采用utf8无BOM格式编码的形式即可。
具体设置,查看下图:
补充: bom格式处理
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5006')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5006>String st="utf-8的bom格式字符串内容";byte[] bytes = st.getBytes("utf-8");
byte[] strs = new byte[bytes.length - 3];//去掉该内容的bom标示
int j = 0;
if (bytes[0] == -17 && bytes[1] == -69 && bytes[2] == -65) {//bom文件格式头字节
for (int i = 3; i < bytes.length; i ) {
strs[j] = bytes[i];
j ;
}
原因这个也是编码问题与php中的bom 格式编码也是一样一样的呀。
您可能感兴趣的文章:
java中JSONException: java.lang.String cannot be converted to JSONObject
看看String类的构造方法
nodejs中使用mysql数据库的示例
A Study of WebRTC Security
java string.ToCharArray()的使用方法
java substring()函数删除指定字符串
The type XXX cannot be resolved. It is indirectly referenced from required .class files错误
Overloading overriding runtime type and object ori
关于java 的 Classpath全解<java入门之一>
TIJ swot_JAVA的初学者(四)