值无法转换为JSON数组

时间:2018-10-26 16:34:46

标签: java android

这是给我问题的函数:

    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的内容,该字符串的String为JSON。

2 个答案:

答案 0 :(得分:1)

从异常中可以明显看出,URL返回的 JSON字符串的类型为JSONObject,而不是JSONArray

  

无法将类型为org.json.JSONObject的值{应该是的值}转换为JSONArray

JSON对象将以{开头,并以}

  

{        “ KEY1”:“ VALUE1”,        “ KEY2”:“ VALUE2”      }

,JSON数组将以[开头,并以]结尾。

  

[
     {“ KEY1”:“ VALUE1”,“ KEY2”:“ VALUE2”},{“ KEY1”:“ VALUE1”,“ KEY2”:“ VALUE2”}
  ]

因此您将收到此异常,因为您正在尝试将JSON对象转换为JSON数组。

答案 1 :(得分:0)

到Deepak Gunasekaran

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)