【问题标题】:Type JSONObject cannot be converted to JSONArray in Volley类型 JSONObject 无法在 Volley 中转换为 JSONArray
【发布时间】:2015-12-12 01:30:42
【问题描述】:

我正在尝试从 JSON 提要中读取注册号。

{"error":false,"0":[{"registration_number":"HJ-1756"},{"registration_number":"ABD-1234"}]}

但是我很难用下面的代码来做。

JsonArrayRequest req = new JsonArrayRequest(Constants.URL_VEHICLE_REG_JSON,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());

                        try {
                            // Parsing json array response
                            // loop through each json object
                            jsonResponse = "";

                            // get an array from the JSON object
                            JSONArray res = (JSONArray) response.get(1);

                            for (int i = 0; i < res.length(); i++) {

                                //JSONObject car = (JSONObject) response.get(i);
                                JSONObject innerObj = (JSONObject)res.get(i);

                                String registrationNumber = innerObj.getString("registration_number");

                                jsonResponse += "registration_number: " + registrationNumber + "\n\n";

                                Log.d(TAG, jsonResponse);

                                items.add(registrationNumber);
                            }



                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
                        adapter.notifyDataSetChanged();

                        hideDialog();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                Log.d(TAG, "Error: " + error.getMessage());
                hideDialog();
            }
        });

我也尝试将 Listener 更改为 Listener 和响应。但它没有用。我知道我得到 JSONObject 结果。但是我哪里出错了?

【问题讨论】:

    标签: android json android-volley


    【解决方案1】:

    首先,因为您的 JSON 响应是 JSONObject,所以您应该使用 Volley 的 JsonObjectRequest 而不是 JsonArrayRequest

    那么,假设你成功获取了 JSONObject 响应数据,你可以使用下面的示例代码:

                if (jsonObject != null && !jsonObject.isNull("0")) {
                    JSONArray jsonArray = jsonObject.getJSONArray("0");
                    if (jsonArray != null && jsonArray.length() > 0) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObj = jsonArray.getJSONObject(i);
                            if (jsonObj != null && !jsonObj.isNull("registration_number")){
                                String regNum = jsonObj.getString("registration_number");
                            }
                        }
                    }
                }
    

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      你试试这个代码

                  JSONArray jsonArray = jsonObject.getJSONArray("0");
                  if (jsonArray != null) {
                      for (int i = 0; i < jsonArray.length(); i++) {
                          JSONObject jsonObj = jsonArray.getJSONObject(i);
      
                              String regNum = jsonObj.getString("registration_number");
                          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-05
        • 1970-01-01
        • 2021-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-12
        相关资源
        最近更新 更多