亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

值無法轉換為 JSON 數組

值無法轉換為 JSON 數組

UYOU 2021-12-10 12:32:38
這是給我帶來問題的功能:    public String URLToJson() {    String result = "";    String jsonString = ReadingURL(" here goes my URL that reads a JSON ");    JSONObject jsonResult = null;    try {        jsonResult = new JSONObject(jsonString);        JSONArray data = jsonResult.getJSONArray("Configuracion");        if (data != null) {            for (int i = 0; i <= data.length(); i++) {                result =  result + "Dirección: " + data.getJSONObject(i).getString("Direccion") + "\n";                result =  result + "Cédula: " + data.getJSONObject(i).getString("Cedula") + "\n";                result =  result + "Nombre: : " + data.getJSONObject(i).getString("Nombre") + "\n";                result =  result + "Teléfono : " + data.getJSONObject(i).getString("Telefono") + "\n";                result =  result + "Hacienda: " + data.getJSONObject(i).getString("Hacienda") + "\n";            }        }        return result;    }catch (JSONException e){        e.printStackTrace();        return "Error Reading JSON Data";    }}然后出現了這個:`W/System.err: org.json.JSONException: Value {"Direccion":"Somewhere","Cedula":"111111","Nombre":"Something","Telefono":"2222-2440","Hacienda":"Something"} at Configuracion of type org.json.JSONObject cannot be converted to JSONArray        at org.json.JSON.typeMismatch(JSON.java:100)W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:588)        at com.example.user.mypos.PrintManager.URLToJson(PrintManager.java:977)W/System.err:     at com.example.user.mypos.PrintManager$4.run(PrintManager.java:917)        at java.lang.Thread.run(Thread.java:818)W/System.err: org.json.JSONException: Value { the values that are supposed to be }  of type org.json.JSONObject cannot be converted to JSONArray`ReadingURL 基本上讀取 URL 的內容,該內容在字符串中包含 JSON。
查看完整描述

2 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

從異常中可以清楚地看出,URL 返回JSON 字符串的類型是 JSONObject 而不是 JSONArray。

值 { 應該是 } 類型 org.json.JSONObject 的值無法轉換為 JSONArray

JSON 對象將以 { 開頭 & 以 } 結尾

{ "KEY1":"VALUE1", "KEY2":"VALUE2" }

并且 JSON 數組將以 [ 開頭并以 ] 結尾。

[
{"KEY1":"VALUE1","KEY2":"VALUE2"},{"KEY1":"VALUE1","KEY2":"VALUE2"}
]

因此,您收到此異常是因為您正在嘗試將 JSON 對象轉換為 JSON 數組。


查看完整回答
反對 回復 2021-12-10
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

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";

    }

}

現在它只顯示


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)


查看完整回答
反對 回復 2021-12-10
  • 2 回答
  • 0 關注
  • 229 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號