0

我正在使用本教程:http ://www.androidhive.info/2012/10/android-multilevel-listview-tutorial/

在这里,他创建了 3 个不同的 JSON,

首先,获取专辑列表

、获取专辑下的歌曲列表

、显示单曲信息

但我需要在这里做一个小改动,而不是 3 个不同的 JSON,我想为专辑和歌曲使用单个 JSON,并且我也创建了我的演示 JSON,我能够获取专辑列表但没有得到列表歌曲,我的 JSON 看起来像这样,我使用的代码与他为客户端编写的代码相同:

我的.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"
        }
    ]
}
]

日志猫:

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)

注意:我可以获取专辑列表,但每当我点击任何专辑时都没有歌曲

4

2 回答 2

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

      }
    }
于 2013-06-18T06:58:58.543 回答
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"
        }
    ]
}
于 2013-06-18T07:03:21.927 回答