我正在使用本教程:http://www.androidhive.info/2012/10/android-multilevel-listview-tutorial/
在这里他创造了3种不同的JSON,
首先,以获取相册列表
第二次,以获取相册
下的歌曲列表第三次,以显示单曲信息
但我需要在这里做一个小改动,而不是3种不同的JSON ,我想要使用单个JSON用于两个专辑&歌曲,我也创建了我的演示JSON,我能够获得专辑列表但没有获得歌曲列表,我的JSON看起来像这样,我使用相同的代码正如他为客户端撰写的那样:
my.json:
[
{
"id": 1,
"name": "127 Hours",
"album": "127 Hours",
"songs": [
{
"id": 1,
"name": "Never Hear Surf Music Again",
"duration": "5:52"
},
{
"id": 2,
"name": "The Canyon",
"duration": "3:01"
}
]
}
]
logcat的:
06-18 06:36:56.318: D/dalvikvm(793): GC_CONCURRENT freed 143K, 7% free 3745K/4012K, paused 6ms+127ms, total 243ms
06-18 06:36:56.399: I/Choreographer(793): Skipped 37 frames! The application may be doing too much work on its main thread.
06-18 06:36:56.689: I/Choreographer(793): Skipped 66 frames! The application may be doing too much work on its main thread.
06-18 06:36:57.508: D/Track List JSON:(793): [
06-18 06:36:57.508: D/Track List JSON:(793): {
06-18 06:36:57.508: D/Track List JSON:(793): "id":1,
06-18 06:36:57.508: D/Track List JSON:(793): "name":"127 Hours",
06-18 06:36:57.508: D/Track List JSON:(793): "album":"127 Hours",
06-18 06:36:57.508: D/Track List JSON:(793): "songs":[
06-18 06:36:57.508: D/Track List JSON:(793): {
06-18 06:36:57.508: D/Track List JSON:(793): "id":1,
06-18 06:36:57.508: D/Track List JSON:(793): "name":"Never Hear Surf Music Again",
06-18 06:36:57.508: D/Track List JSON:(793): "duration":"5:52"
06-18 06:36:57.508: D/Track List JSON:(793): },
06-18 06:36:57.508: D/Track List JSON:(793): {
06-18 06:36:57.508: D/Track List JSON:(793): "id":2,
06-18 06:36:57.508: D/Track List JSON:(793): "name":"The Canyon",
06-18 06:36:57.508: D/Track List JSON:(793): "duration":"3:01"
06-18 06:36:57.508: D/Track List JSON:(793): }
06-18 06:36:57.508: D/Track List JSON:(793): ]
06-18 06:36:57.508: D/Track List JSON:(793): }
06-18 06:36:57.508: D/Track List JSON:(793): ]
06-18 06:36:57.668: W/System.err(793): org.json.JSONException: Value [{"id":1,"songs":[{"id":1,"duration":"5:52","name":"Never Hear Surf Music Again"},{"id":2,"duration":"3:01","name":"The Canyon"}],"album":"127 Hours","name":"127 Hours"}] of type org.json.JSONArray cannot be converted to JSONObject
06-18 06:36:57.668: W/System.err(793): at org.json.JSON.typeMismatch(JSON.java:111)
06-18 06:36:57.668: W/System.err(793): at org.json.JSONObject.<init>(JSONObject.java:158)
06-18 06:36:57.788: W/System.err(793): at org.json.JSONObject.<init>(JSONObject.java:171)
06-18 06:36:57.888: W/System.err(793): at com.example.androidhive.TrackListActivity$LoadTracks.doInBackground(TrackListActivity.java:155)
06-18 06:36:57.898: W/System.err(793): at com.example.androidhive.TrackListActivity$LoadTracks.doInBackground(TrackListActivity.java:1)
06-18 06:36:57.898: W/System.err(793): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-18 06:36:57.898: W/System.err(793): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-18 06:36:57.898: W/System.err(793): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-18 06:36:57.898: W/System.err(793): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-18 06:36:57.909: W/System.err(793): at java.lang.Thread.run(Thread.java:856)
注意:我可以获取相册列表,但每当我点击任何相册都没有收到歌曲
答案 0 :(得分:0)
因为你的json数据以方括号开头意味着它是一个jsonarray,你需要用jsonarray开始解析它。
此代码可以帮助您..
JSONArray array = new JSONArray("Your_json_string");
for(int i=0;i<array.length();i++){
System.out.println("id:"+array.getJSONObject(i).getString("id"));
System.out.println("name:"+array.getJSONObject(i).getString("name"));
System.out.println("album:"+array.getJSONObject(i).getString("album"));
JSONArray innerArray = getJSONObject(i)..getJSONArray("songs");
for(int k=0;k<innerArray.length();k++){
JsonObject innerObj = innerArray.getJSONObject(k);
System.out.println("id:"+innerObj.getString("id"));
System.out.println("Name:"+innerObj.getString("name"));
System.out.println("duration:"+innerObj.getString("duration"));
}
}
答案 1 :(得分:0)
您正在尝试将JSON数组转换为JSON对象,这就是问题所在。
org.json.JSONException: Value [.......] of type org.json.JSONArray cannot be converted to JSONObject
下面:
JSONObject jObj = new JSONObject(json);
您的JSON字符串实际上是一个数组,而不是一个对象,因为它以[]
开头。
如果您想要处理一个对象,请更改它并使其以{}
开头,并且您已完成(或将其转换为JSONArray并获得第一个值)!
编辑:
这应该有效:
{
"id": 1,
"name": "127 Hours",
"album": "127 Hours",
"songs": [
{
"id": 1,
"name": "Never Hear Surf Music Again",
"duration": "5:52"
},
{
"id": 2,
"name": "The Canyon",
"duration": "3:01"
}
]
}