2013-06-18 47 views
0

我使用本教程:http://www.androidhive.info/2012/10/android-multilevel-listview-tutorial/单JSON获取多个记录

在这里,他创造了3个不同的JSONs,

首先,获取相册

的列表,以获取歌曲下的专辑列表

第三,显示单曲信息

,但我在这里需要一个小的变化,而不是3个不同的,不同的JSONs,我想使用单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

你可以在网上找到许多JSON的浏览器的观众,我使用JSON文件时使用他们制造了很多: HTTP:// jsonviewer。 stack.hu/ –

+0

这是什么问题?邮政编码和logcat! – Enrichman

+0

看来你的json是有效的 - 在http://jsonlint.com/检查你可以发布你的代码? –

回答

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

     } 
    } 
+0

他要求解析json数据吗? – Raghunandan

0

你试图将一个JSON数组转换成JSON对象,这就是问题所在。

org.json.JSONException: Value [.......] of type org.json.JSONArray cannot be converted to JSONObject 

这里:

JSONObject jObj = new JSONObject(json); 

你的JSON字符串实际上是一个数组,而不是一个对象,因为它与[]开始。

如果你想处理一个对象改变它,并使它开始{},你完成了(或将其转换为JSONArray并获得firts值)!


编辑:

这应该工作:

{ 
    "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" 
     } 
    ] 
} 
+0

请告诉我我的JSON应该是什么样子,因为我不想在我的代码 –

+0

中修改编辑。尝试这个! – Enrichman

+0

尝试甚至没有得到类别列表 –