【问题标题】:Convert Json object to an array将 Json 对象转换为数组
【发布时间】:2018-09-12 13:49:45
【问题描述】:

我在修改我的 android studio 代码以将 Json 对象转换为数组时需要帮助。下面是我的日志猫和实际代码中的错误。

这是我在 logcat 中收到的错误消息:

04-03 12:01:16.727 19993-19993/com.errandit E/Volley: com.android.volley.ParseError: org.json.JSONException: Value {"data":[{"errand":"Shopping","charges":"500"},{"errand":"House Assistance","charges":"7000"},{"errand":"Pick - Up","charges":"2500"}],"success":1,"message":" 0 records found"} of type org.json.JSONObject cannot be converted to JSONArray

**我正在尝试使用以下方法从我的 wamp 服务器中获取 json 数组。 **

private void getData(){
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading");
        progressDialog.show();
        final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                for (int i = 0; i < response.length(); i++) {
                   try {
                        JSONObject jsonObject = response.getJSONObject(i);
                        Service service = new Service();
                        service.setName(jsonObject.getString("errand"));
                        service.setCharges(jsonObject.getInt("charges"));
                        serviceList.add(service);
                    } catch (JSONException e) {
                        e.printStackTrace();
                        progressDialog.dismiss();
                    }
                }
                adapter.notifyDataSetChanged();
                progressDialog.dismiss();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Volley",error.toString());
                progressDialog.dismiss();
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonArrayRequest);

    }

【问题讨论】:

    标签: android arrays json android-volley


    【解决方案1】:

    如下更改,因为您正在获取 JSONObject,其中有一个名为“data”的 JSONArray。

    JsonObjectRequest jsonObjRequest = new JsonObjectRequest
            (Request.Method.POST, url, jsonObj, new Response.Listener<JSONObject>()
            {
                @Override
                public void onResponse(JSONObject mResponse)
                {
          JSONArray response=mResponse.optJSONArray("data")
    
                     for (int i = 0; i < response.length(); i++) {
                       try {
                            JSONObject jsonObject = response.getJSONObject(i);
                            Service service = new Service();
                            service.setName(jsonObject.getString("errand"));
                            service.setCharges(jsonObject.getInt("charges"));
                            serviceList.add(service);
                        } catch (JSONException e) {
                            e.printStackTrace();
    
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error)
                {
                    Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show();
                }
            });
    
    // Add the request to the RequestQueue.
    queue.add(jsonObjRequest);
    

    【讨论】:

      猜你喜欢
      • 2012-07-21
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多