Android Studio 不是基元数组:类组织.json. JSON数组

l7wslrjt  于 2022-12-19  发布在  Android
关注(0)|答案(1)|浏览(128)

第一个月
这是我的Json文件,但我想使用Volley laiberry,但不使用原始数组:json.jSONArray帮助我们的java代码是

RequestQueue queue = Volley.newRequestQueue(this);
        String url ="http://loclhost:1111/json";
         JsonObjectRequest jsonArryRequest =new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
             @Override
             public void onResponse(JSONObject response) {

                 try {

                    JSONArray jsonArray = new JSONArray(response.getJSONArray("records").getJSONObject(1));

                 } catch (JSONException e) {
                     e.printStackTrace();
                     Log.d("Papa","Ex - "+e.getMessage());
                 }

             }
         }, new Response.ErrorListener() {
             @Override
             public void onErrorResponse(VolleyError error) {
                 String errorm = error.getMessage();
                 Log.d("papa", "error : "+errorm);

             }
         });

         queue.add(jsonArryRequest);
igetnqfo

igetnqfo1#

您正在初始化一个新的jsonArray,这不是正确的方法
而不是这个

JSONArray jsonArray = new JSONArray(response.getJSONArray("records").getJSONObject(1));

这样做

JSONArray jsonArray = response.getJSONArray("records");
JSONObject jsonObject = jsonArray.getJSONObject(1));

通过这种方式,您还可以将第二个json对象放入jsonObject变量中

相关问题