2 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊
從異常中可以清楚地看出,URL 返回的JSON 字符串的類型是 JSONObject 而不是 JSONArray。
值 { 應(yīng)該是 } 類型 org.json.JSONObject 的值無法轉(zhuǎn)換為 JSONArray
JSON 對(duì)象將以 { 開頭 & 以 } 結(jié)尾
{ "KEY1":"VALUE1", "KEY2":"VALUE2" }
并且 JSON 數(shù)組將以 [ 開頭并以 ] 結(jié)尾。
[
{"KEY1":"VALUE1","KEY2":"VALUE2"},{"KEY1":"VALUE1","KEY2":"VALUE2"}
]
因此,您收到此異常是因?yàn)槟趪L試將 JSON 對(duì)象轉(zhuǎn)換為 JSON 數(shù)組。

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊
public String URLToJson() {
String result = "";
String jsonString = ReadingURL("http://deliciasmarinas.avancari.co.cr/app/tiquete.php?factura=414696772");
JSONObject jsonResult = null;
try {
jsonResult = new JSONObject(jsonString);
for (int i = 0; i <= jsonResult.length(); i++) {
result = result + "Dirección: " + jsonResult.get("Direccion") + "\n";
result = result + "Cédula: " + jsonResult.get("Cedula") + "\n";
result = result + "Nombre: : " + jsonResult.get("Nombre") + "\n";
result = result + "Teléfono : " + jsonResult.get("Telefono") + "\n";
result = result + "Hacienda: " + jsonResult.get("Hacienda") + "\n";
}
return result;
}catch (JSONException e){
e.printStackTrace();
return "Error Reading JSON Data";
}
}
現(xiàn)在它只顯示
W/System.err: org.json.JSONException: No value for Direccion
at org.json.JSONObject.get(JSONObject.java:389)
W/System.err: at com.example.user.mypos.PrintManager.URLToJson(PrintManager.java:978)
at com.example.user.mypos.PrintManager$4.run(PrintManager.java:917)
at java.lang.Thread.run(Thread.java:818)
添加回答
舉報(bào)