嗨我在2.3中遇到了Android JSON解析器的问题,在4.0>中一切正常。我试图把“测试”放在我的所有JSON字段中,但问题仍然存在。org.json.JSONException:无法将类型java.lang.String的值转换为JSONObject
这里是我的代码:
URL url = new URL(c.getString(R.string.url_ws) + url_ws);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
OutputStreamWriter request = new OutputStreamWriter(connection.getOutputStream());
request.write("&test=test");
request.flush();
request.close();
String line;
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
String json = sb.toString().trim();
try {
JSONObject obj = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
Log.d(Tools.TAG+"/debug JSONException", e.toString());
return null;
}
这里是我的例外
debug JSONException(4184): org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
我看到“价值”之间的双重空间“的”,所以我想这个问题是一个空字符串,但我不明白。
谢谢。
编辑
这是我JSON一个编码错误,
char a = json.substring(0, 1).charAt(0);
int ascii = (int)a;
Tools.myLog(">"+ascii+"<");
我发现65279字符
SOLUTION
Why is  appearing in my HTML?
编码UTF-8出BOM。
http://stackoverflow.com/questions/17792779/what-is-the-error-表示功能于androidjson误差的解析数据-ORG-JSON-jsonexcep/17793283#17793283。类似!。发布你的json。 – Raghunandan
JSONArray obj = new JSONArray(json);使用这个而不是JSONObject obj = new JSONObject(json); – Giant
我的JSON是不是数组: { “bHasError”:假的, “sSessionId”: “5e10be14c408732179aa4731899882fd”, “iMembreId”:7510064 “iCurrentDate”:1374565720 .... –