我的应用无法从服务器获取数据。
我在互联网上找到的每个解决方案都不起作用。
private static final String KEY_DATA = "data";
HashMap<String, String> temp = new HashMap<>();
temp.put("nome",textoDigitado);
HttpJsonParser httpJsonParser = new HttpJsonParser();
JSONObject jsonObject = httpJsonParser.makeHttpRequest(BASE_URL+"fetch_single_bar.php","GET",temp);
JSONArray bares_json;
if (success == 1){
bares_json = jsonObject.getJSONArray(KEY_DATA);
}
W/System.err: org.json.JSONException: Value {"id":17,"nome":"bar teste","cidade":"cidade teste","estado":"estado teste","conectados":"0"} at data of type org.json.JSONObject cannot be converted to JSONArray
导航器中搜索的响应是:
{"success":1,"data":{"id":17,"nome":"bar teste","cidade":"cidade teste","estado":"estado teste","conectados":"0"}}
最佳答案
是的,您可以将JSONObject
解析为JSONArray
。但首先,使用 Volley 更容易获得响应(这是一个建议)。
您只需要做的是JSONArray array = (JSONArray) jsonObject.get("KEY_DATA");
。
jsonObject.get("KEY_DATA")
返回一个对象。
Volley 是一个异步库,它可以帮助您处理服务器响应,并且比我在其他库中看到的要容易得多。
关于java - JSONObject 无法转换为 JSONArray , jsonObject.getJSONArray(KEY_DATA) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57592421/