首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSONObject不能转换为JSONArray - Android

JSONObject不能转换为JSONArray - Android
EN

Stack Overflow用户
提问于 2016-09-30 12:45:36
回答 3查看 135关注 0票数 0

我想从这些数据中获取数据到android。但是它失败了,并且get错误org.json.JSONObject不能转换为JSONArray。我不知道错误在哪里我很纠结于这些东西。请帮忙..。谢谢

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
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();

    }
}

StackTrace

代码语言:javascript
复制
    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)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-09-30 13:16:06

您的代码出现了问题:

代码语言:javascript
复制
String url=c.getJSONObject("icons").getString("url");

解决方案: 1)使用"items“代替"TAG_HASIL”,如下所示:

代码语言:javascript
复制
names = json.getJSONArray("items");

第二:代替

代码语言:javascript
复制
String url=c.getJSONObject("icons").getString("url");  

用途:

代码语言:javascript
复制
JSONObject job=c.getJSONArray("icons").get[0].getString("url"); 
String url=job.getString("url");
票数 1
EN

Stack Overflow用户

发布于 2016-09-30 12:59:11

类别是一个JSON Object。其中itemsJSON Array。您不是在考虑categories,而是直接尝试迭代items

使用gson以获得更好的灵活性。你不需要这个手动转换。将以下内容添加到依赖项中

代码语言:javascript
复制
dependencies {
    compile 'com.solidfire.code.gson:gson:2.6.2'
}

使用gson格式插件看这段视频解释如何实现此插件

现在,相应地重新格式化代码。通过这个有用的链接来解释这个场景并简化编码。

票数 1
EN

Stack Overflow用户

发布于 2016-09-30 12:53:57

请使用您可以从这里获得的net.sf.json库:http://java2s.com/Code/Jar/j/Downloadjsonlibjar.htm

代码语言:javascript
复制
String aa =json.toString();    // json is jsonObject
JSONArray jarray = (JSONArray) JSONSerializer.toJSON(aa);

jarray现在是一个json数组。

如果您有任何classNotFound错误,只需下载支持jars即可。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39791851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档