• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

JsonArray cannot be converted in JsonObject error

I try to make a graph with the Json date's recived from an API and i can't pass by this error. The URL is 100% VALID, i get the entire file. This is the URL: https://apiv2.bitcoinaverage.com/indices/global/ticker/BTCRON. This is the method for the request and plot of the graph:
Java:
private void drawChart() {

    String tag_string_req = "req_chart";

    StringRequest strReq = new StringRequest(Method.GET, BTC_URL,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    Log.d(TAG, "Response graph dates: " + response);

                    try {
                        JSONObject jsonObject = new JSONObject(response);

                        JSONArray jsonArray = jsonObject.getJSONArray("open");

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

                            int hour = jsonObject.getInt("hour");
                            String date = jsonObject.getString("day");

                            x.add(new Entry(hour, i));
                            y.add(date);


                       }
                        LineDataSet set1 = new LineDataSet(x, "NAV Data Value");
                        set1.setLineWidth(1.5f);
                        set1.setCircleRadius(4f);
                        LineData data = new LineData(y, set1);
                        mChart.setData(data);
                        mChart.invalidate();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Error: " + error.getMessage());
        }
    });
    strReq.setRetryPolicy(new RetryPolicy() {

        @Override
        public void retry(VolleyError arg0) throws VolleyError {
        }

        @Override
        public int getCurrentTimeout() {
            return 0;
        }

        @Override
        public int getCurrentRetryCount() {
            return 0;
        }
    });
    strReq.setShouldCache(false);
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
 
Thread moved to Android Development forum.

Looking at the JSON data at that URL, you're getting this error because the "open" element isn't an array

Code:
"open": {
       "hour": 26223.29,
       "day": 26097.95,
       "week": 29721.60,
       "month": 33248.89,
       "month_3": 32488.29,
       "month_6": 77617.15,
       "year": 10280.31
   }

That's not an array, it's a data structure containing a number of other elements. Arrays in JSON are expressed by using "[ ]" notation.
Your JSON is syntactically correct, but the code is giving you an error because it's trying to interpret something as an array, which isn't.
Have a look at this to learn what a JSON array looks like:

https://www.w3schools.com/js/js_json_arrays.asp
 
Back
Top Bottom