我想从这个数据中获取数据到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)
答案 0 :(得分:1)
类别是JSON Object
。其中items
为JSON Array
。您没有考虑categories
,而是直接尝试迭代items
使用gson
可获得更好的灵活性。您不需要这种手动转换。
将以下内容添加到您的依赖项
dependencies {
compile 'com.solidfire.code.gson:gson:2.6.2'
}
使用gson格式插件Look at this video解释如何实现此插件
现在重新格式化您的代码。通过this helpful link来解释这个场景并简化编码。
答案 1 :(得分:1)
您的代码中存在问题:
String url=c.getJSONObject("icons").getString("url");
解决方案: 1)使用&#34;项目&#34;代替&#34; TAG_HASIL&#34;如下所示:
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");
答案 2 :(得分:0)
请使用您可以从此处获取的net.sf.json
库:
http://java2s.com/Code/Jar/j/Downloadjsonlibjar.htm
String aa =json.toString(); // json is jsonObject
JSONArray jarray = (JSONArray) JSONSerializer.toJSON(aa);
jarray现在是一个json数组。
如果你得到任何classNotFound错误只需下载支持jar。