值无法转换为 JSON 数组

这是给我带来问题的功能:


    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。


UYOU
浏览 182回答 2
2回答

素胚勾勒不出你

从异常中可以清楚地看出,URL 返回的JSON 字符串的类型是 JSONObject 而不是 JSONArray。值 { 应该是 } 类型 org.json.JSONObject 的值无法转换为 JSONArrayJSON 对象将以 { 开头 & 以 } 结尾{ "KEY1":"VALUE1", "KEY2":"VALUE2" }并且 JSON 数组将以 [ 开头并以 ] 结尾。[{"KEY1":"VALUE1","KEY2":"VALUE2"},{"KEY1":"VALUE1","KEY2":"VALUE2"}]因此,您收到此异常是因为您正在尝试将 JSON 对象转换为 JSON 数组。

江户川乱折腾

public String URLToJson() {&nbsp; &nbsp; String result = "";&nbsp; &nbsp; String jsonString = ReadingURL("http://deliciasmarinas.avancari.co.cr/app/tiquete.php?factura=414696772");&nbsp; &nbsp; JSONObject jsonResult = null;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; jsonResult = new JSONObject(jsonString);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i <= jsonResult.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result =&nbsp; result + "Dirección: " + jsonResult.get("Direccion") + "\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result =&nbsp; result + "Cédula: " + jsonResult.get("Cedula") + "\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result =&nbsp; result + "Nombre: : " + jsonResult.get("Nombre") + "\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result =&nbsp; result + "Teléfono : " + jsonResult.get("Telefono") + "\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result =&nbsp; result + "Hacienda: " + jsonResult.get("Hacienda") + "\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }catch (JSONException e){&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; return "Error Reading JSON Data";&nbsp; &nbsp; }}现在它只显示W/System.err: org.json.JSONException: No value for Direccion&nbsp; &nbsp; &nbsp; &nbsp; at org.json.JSONObject.get(JSONObject.java:389)W/System.err:&nbsp; &nbsp; &nbsp;at com.example.user.mypos.PrintManager.URLToJson(PrintManager.java:978)&nbsp; &nbsp; &nbsp; &nbsp; at com.example.user.mypos.PrintManager$4.run(PrintManager.java:917)&nbsp; &nbsp; &nbsp; &nbsp; at java.lang.Thread.run(Thread.java:818)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java