Java JSONObject无法转换为JSONArray-Android

Java JSONObject无法转换为JSONArray-Android,java,android,json,Java,Android,Json,我想从这个数据中提取数据到android。但是它失败并得到错误org.json.JSONObject无法转换为JSONArray。我不知道哪里出了错,我一直在挣扎。请帮忙。。多谢各位 { "categories" : { "href" : "https://api.spotify.com/v1/browse/categories?offset=0&limit=20", "items" : [ { "href" : "https://api.spotify.

我想从这个数据中提取数据到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();

    }
}

请使用
net.sf.json
库,您可以从这里获得:

jarray现在是一个json数组


如果您遇到任何classNotFound错误,请下载支持JAR

请使用
net.sf.json
库,您可以从这里获得:

jarray现在是一个json数组


如果您遇到任何classNotFound错误,请下载支持JAR

Categories是一个
JSON对象
。其中
JSON数组
。您没有考虑
类别
,而是直接尝试迭代


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

dependencies {
    compile 'com.solidfire.code.gson:gson:2.6.2'
}
使用gson格式插件解释如何实现此插件


现在,相应地重新格式化代码。仔细阅读解释此场景并简化编码。

类别是一个
JSON对象。其中
JSON数组
。您没有考虑
类别
,而是直接尝试迭代


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

dependencies {
    compile 'com.solidfire.code.gson:gson:2.6.2'
}
使用gson格式插件解释如何实现此插件


现在,相应地重新格式化代码。仔细阅读解释此场景并简化编码。

您在代码行中遇到了问题:

String url=c.getJSONObject("icons").getString("url");
解决方案: 1) 使用“项目”代替“标签”,如下所示:

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");

您的代码行中有问题:

String url=c.getJSONObject("icons").getString("url");
解决方案: 1) 使用“项目”代替“标签”,如下所示:

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");

使用此url查看您的json如何:TAG_HASIL的字符串值是多少?categories@Prashant您可以发布错误日志跟踪吗?ok@MultidotsSolutions使用此url查看您的json如何:TAG_HASIL的字符串值是多少?categories@Prashant您可以发布错误日志跟踪吗?ok@MultidotsSolutionsCorrect,您可以用作get[0]。getString(“url”);正确,可以用作get[0]。getString(“url”);