【问题标题】:Getting JSONArray result in "org.json.JSONException: Value{data}"在“org.json.JSONException: Value{data}”中获取 JSONArray 结果
【发布时间】:2019-02-19 12:10:11
【问题描述】:

我正在使用 Volley 请求 unsplash API,但是当我尝试以 JsonObjectRequest 请求它时,它没有给我任何错误,但我知道这是错误的方法,因为我收到的数据是 JSONArray

我的方法

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
            Request.Method.GET,
            URL,
            null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    if(response!=null){
                    // Process the JSON
                        try{
                            // Loop through the array elements
                            for (int i = 0; i < response.length(); i++) {
                                JSONObject js = response.getJSONObject(i);
                                Log.d("TESTING",js.getString("id"));
                            }
                        p.dismiss();
                    }catch (JSONException e){
                        e.printStackTrace();
                    }
                }}
            },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error){
                    Log.d("TESTING",error.getMessage());
                }
            });
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);

日志 由于 JSONArray 太大,无法在此处发布,这是格式

D/TESTING: org.json.JSONException: Value {"total": 44760,
"total_pages": 4476,
"results":[{JSONObjects}]}

如果您想查看 JSONArray,请点击链接“https://api.unsplash.com/search/photos?query=wood&amp;client_id=ACESS_KEY_REQUIRED

我也看过this的问题,但这完全不同

您只需创建一个帐户here即可申请密钥。

【问题讨论】:

    标签: android arrays json


    【解决方案1】:

    你需要先创建 jsonObject。您请求JSONArray,但您的服务器响应为JSONObject。尝试StringRequest 获取JSONObject

    试试这个:

        StringRequest stringRequest = new StringRequest(Request.Method.GET,
                URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
    
                        try {
                            // Loop through the array elements
                            JSONObject jsonObject = new JSONObject(response);
                            JSONArray jsonArray = jsonObject.getJSONArray("results");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject js = jsonArray.getJSONObject(i);
                                Log.d("TESTING", js.getString("id"));
                            }
                            p.dismiss();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
    
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                 Log.d("TESTING",error.getMessage()); 
            }
        })
    };
    
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
    

    希望这会奏效。

    【讨论】:

    • JSONObject jsonObject = new JSONObject(response);无法解析构造函数 'JSONObject(org.json.JSONArray)'
    • 成功了,非常感谢!但是在结果的每个对象中都有诸如“id”、“height”等和“urls”之类的字段,现在这些 url 类似于:“urls”:{“raw”:“data”、“full”:“data”。 .....}知道如何获得这些“原始”和“完整”字段
    • 我做到了,再次感谢您的时间和建议
    【解决方案2】:

    你是对 json 对象的请求,但是你调用 jsonarrayrequest 请求时间,所以 JsonException 来了

        JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,url,
                null, new Response.Listener<JSONObject>() {
       @Override
       public void onResponse(JSONObject response) {
           try {
               JSONArray jsonArray = response.getJSONArray("results");
                for (int i = 0; i < jsonArray.length(); i++) {
                  JSONObject js = jsonArray.getJSONObject(i);
                   Log.d("TESTING",js.getString("id"));
    }
           } catch (JSONException e) {
               e.printStackTrace();
           }
       }
    }, new Response.ErrorListener() {
       @Override
       public void onErrorResponse(VolleyError error) {
           VolleyLog.e("Error: ", error.getMessage());
       }
    });
    

    【讨论】:

    • 它不工作 Logcat: " E/Volley: [1] 8.onErrorResponse: Error: "
    • 你已经获得了我为 Post 方法发布的方法,请根据我的编辑进行更改
    • 你的答案也是使用 GET 但我尝试使用 POST,同样的错误
    • 请检查您的 api
    • 如果我的 API 错误,为什么我的 Exception 中会出现 JSONArray?
    猜你喜欢
    • 1970-01-01
    • 2014-03-20
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多