JSONObject无法转换为JSONArray

JSONObject无法转换为JSONArray

我想从此数据中获取数据到android。但是它失败,并且得到错误org.json.JSONObject无法转换为JSONArray。我不知道哪里出了错。请帮助..谢谢

{
  "categories" : {
    "href" : "https://api.spotify.com/v1/browse/categories?offset=0&limit=20",
    "items" : [ {
      "href" : "https://api.spotify.com/v1/browse/categories/toplists",
      "icons" : [ {
        "height" : 275,
        "url" : "https://t.scdn.co/media/derived/toplists_11160599e6a04ac5d6f2757f5511778f_0_0_275_275.jpg",
        "width" : 275
      } ],
      "id" : "toplists",
      "name" : "Top Lists"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/mood",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/original/mood-274x274_976986a31ac8c49794cbdc7246fd5ad7_274x274.jpg",
        "width" : 274
      } ],
      "id" : "mood",
      "name" : "Mood"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/party",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/links/partyicon_274x274.jpg",
        "width" : 274
      } ],
      "id" : "party",
      "name" : "Party"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/pop",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/derived/pop-274x274_447148649685019f5e2a03a39e78ba52_0_0_274_274.jpg",
        "width" : 274
      } ],
      "id" : "pop",
      "name" : "Pop"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/popculture",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/derived/trending-274x274_7b238f7217985e79d3664f2734347b98_0_0_274_274.jpg",
        "width" : 274
      } ],
      "id" : "popculture",
      "name" : "Trending"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/focus",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/original/genre-images-square-274x274_5e50d72b846a198fcd2ca9b3aef5f0c8_274x274.jpg",
        "width" : 274
      } ],
      "id" : "focus",
      "name" : "Focus"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/rock",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/derived/rock_9ce79e0a4ef901bbd10494f5b855d3cc_0_0_274_274.jpg",
        "width" : 274
      } ],
      "id" : "rock",
      "name" : "Rock"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/indie_alt",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/derived/indie-274x274_add35b2b767ff7f3897262ad86809bdb_0_0_274_274.jpg",
        "width" : 274
      } ],
      "id" : "indie_alt",
      "name" : "Indie"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/edm_dance",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/derived/edm-274x274_0ef612604200a9c14995432994455a6d_0_0_274_274.jpg",
        "width" : 274
      } ],
      "id" : "edm_dance",
      "name" : "EDM/Dance"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/chill",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/derived/chill-274x274_4c46374f007813dd10b37e8d8fd35b4b_0_0_274_274.jpg",
        "width" : 274
      } ],
      "id" : "chill",
      "name" : "Chill"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/dinner",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/original/dinner_1b6506abba0ba52c54e6d695c8571078_274x274.jpg",
        "width" : 274
      } ],
      "id" : "dinner",
      "name" : "Dinner"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/sleep",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/derived/sleep-274x274_0d4f836af8fab7bf31526968073e671c_0_0_274_274.jpg",
        "width" : 274
      } ],
      "id" : "sleep",
      "name" : "Sleep"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/hiphop",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/original/hip-274_0a661854d61e29eace5fe63f73495e68_274x274.jpg",
        "width" : 274
      } ],
      "id" : "hiphop",
      "name" : "Hip Hop"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/workout",
      "icons" : [ {
        "height" : null,
        "url" : "https://t.scdn.co/media/links/workout-274x274.jpg",
        "width" : null
      } ],
      "id" : "workout",
      "name" : "Workout"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/rnb",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/derived/r-b-274x274_fd56efa72f4f63764b011b68121581d8_0_0_274_274.jpg",
        "width" : 274
      } ],
      "id" : "rnb",
      "name" : "RnB"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/country",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/derived/icon-274x274_6a35972b380f65dc348e0c798fe626a4_0_0_274_274.jpg",
        "width" : 274
      } ],
      "id" : "country",
      "name" : "Country"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/folk_americana",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/derived/folk-274x274_ced3f75528ac61faf505863f7d7fae64_0_0_274_274.jpg",
        "width" : 274
      } ],
      "id" : "folk_americana",
      "name" : "Folk & Americana"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/metal",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/original/metal_27c921443fd0a5ba95b1b2c2ae654b2b_274x274.jpg",
        "width" : 274
      } ],
      "id" : "metal",
      "name" : "Metal"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/soul",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/derived/soul-274x274_266bc900b35dda8956380cffc73a4d8c_0_0_274_274.jpg",
        "width" : 274
      } ],
      "id" : "soul",
      "name" : "Soul"
    }, {
      "href" : "https://api.spotify.com/v1/browse/categories/travel",
      "icons" : [ {
        "height" : 274,
        "url" : "https://t.scdn.co/media/derived/travel-274x274_1e89cd5b42cf8bd2ff8fc4fb26f2e955_0_0_274_274.jpg",
        "width" : 274
      } ],
      "id" : "travel",
      "name" : "Travel"
    } ],
    "limit" : 20,
    "next" : "https://api.spotify.com/v1/browse/categories?offset=20&limit=20",
    "offset" : 0,
    "previous" : null,
    "total" : 32
  }
}


这是我的Java.class

class GetKat extends AsyncTask<String,String,String> {
    int sukses = 0;
    int maks=0;
    int TotalTrack=0;
    String id;
    public GetKat() {
        pDialog = new ProgressDialog(CategoryActivity.this);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog.setMessage("Please Wait...");
        pDialog.setCancelable(false);
        pDialog.setIndeterminate(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {
        gridItem item=null;
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("access_token", Token));
        params.add(new BasicNameValuePair("limit", "10"));
        try {
            JSONObject json = jParser.makeHttpRequest(BASE_URL, "GET", params);
            if (json != null) {

                nameList = new ArrayList<HashMap<String,String>>();
                Log.d("Semua Nama: ", json.toString());
                names = json.getJSONArray(TAG_HASIL);
                for(int i = 0; i < maks ;i++)
                {
                    JSONObject c = names.getJSONObject(i);
                    String id = c.getString("id");
                    String name=c.getString("name");
                    String url=c.getJSONObject("icons").getString("url");

                    HashMap<String,String> map = new HashMap<String,String>();

                    item=new gridItem();
                    item.setId(id);
                    item.setImage(url);
                    item.setKategori(name);

                    map.put("id",id);
                    map.put("name",name);
                    map.put("url",url);
                    nameList.add(map);
                    mGridData.add(item);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        pDialog.dismiss();
        CustomGridViewAdapter adapter = new CustomGridViewAdapter(
                CategoryActivity.this,R.layout.grid_item,mGridData);
        if(mGridData.size()>0)
        {
            mGridAdapter.setGridData(mGridData);
            data.setAdapter(adapter);
        }
        else
            Toast.makeText(getApplicationContext(), "No Data Available", Toast.LENGTH_SHORT).show();

    }
}


堆栈跟踪

    W/System.err: org.json.JSONException: Value {"href":"https:\/\/api.spotify.com\/v1\/browse\/categories?offset=0&limit=10","items":[{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/toplists","icons":[{"height":275,"url":"https:\/\/t.scdn.co\/media\/derived\/toplists_11160599e6a04ac5d6f2757f5511778f_0_0_275_275.jpg","width":275}],"id":"toplists","name":"Top Lists"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/mood","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/original\/mood-274x274_976986a31ac8c49794cbdc7246fd5ad7_274x274.jpg","width":274}],"id":"mood","name":"Mood"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/party","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/links\/partyicon_274x274.jpg","width":274}],"id":"party","name":"Party"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/pop","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/derived\/pop-274x274_447148649685019f5e2a03a39e78ba52_0_0_274_274.jpg","width":274}],"id":"pop","name":"Pop"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/popculture","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/derived\/trending-274x274_7b238f7217985e79d3664f2734347b98_0_0_274_274.jpg","width":274}],"id":"popculture","name":"Trending"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/focus","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/original\/genre-images-square-274x274_5e50d72b846a198fcd2ca9b3aef5f0c8_274x274.jpg","width":274}],"id":"focus","name":"Focus"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/rock","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/derived\/rock_9ce79e0a4ef901bbd10494f5b855d3cc_0_0_274_274.jpg","width":274}],"id":"rock","name":"Rock"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/indie_alt","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/derived\/indie-274x274_add35b2b767ff7f3897262ad86809bdb_0_0_274_274.jpg","width":274}],"id":"indie_alt","name":"Indie"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/edm_dance","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/derived\/edm-274x274_0ef612604200a9c14995432994455a6d_0_0_274_274.jpg","width":274}],"id":"edm_dance","name":"EDM\/Dance"},{"href":"https:\/\/api.spotify.com\/v1\/browse\/categories\/chill","icons":[{"height":274,"url":"https:\/\/t.scdn.co\/media\/derived\/chill-274x274_4c46374f007813dd10b37e8d8fd35b4b_0_0_274_274.jpg","width":274}],"id":"chill","name":"Chill"}],"limit":10,"next":"https:\/\/api.spotify.com\/v1\/browse\/categories?offset=10&limit=10","offset":0,"previous":null,"total":32} at categories of type org.json.JSONObject cannot be converted to JSONArray
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:     at org.json.JSON.typeMismatch(JSON.java:100)
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:588)
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:     at com.project.plbtw.songknow.CategoryActivity$GetKat.doInBackground(CategoryActivity.java:159)
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:     at com.project.plbtw.songknow.CategoryActivity$GetKat.doInBackground(CategoryActivity.java:128)
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
09-30 19:58:12.711 17054-19522/com.project.plbtw.songknow W/System.err:     at java.lang.Thread.run(Thread.java:818)

最佳答案

您的代码行中有问题:

String url=c.getJSONObject("icons").getString("url");


解:
1)如下所示,使用“项目”代替“ TAG_HASIL”:

names = json.getJSONArray("items");


第二:代替

String url=c.getJSONObject("icons").getString("url");


采用 :

JSONObject job=c.getJSONArray("icons").get[0].getString("url");
String url=job.getString("url");

09-19 00:30