java - 无法解析复杂的 json

标签 java android json

我正在尝试做一个向下钻取的导航应用程序。我有一张专辑名称列表。单击专辑时,它将打开该特定专辑的歌曲列表。问题是当我单击专辑时,它也会打开所有其他专辑的所有歌曲。

请帮忙。下面是我的编码。

相册 JSON:

[
    {   "albumid": "1",
        "name":"127 Hours",
        "songs_count":"14"
    },
    {   "albumid": "2",
        "name":"Adele 21",
        "songs_count":"11"
    },
    {   "albumid": "3",
        "name":"Lana Del Rey - Born to Die",
        "songs_count":"12"
    },
    {   "albumid": "4",
        "name":"Once",
        "songs_count":"13"
    },
    {   "albumid": "5",
        "name":"Away We Go",
        "songs_count":"13"
    },
    {   "albumid": "6",
        "name":"Eminem Curtain Call",
        "songs_count":"14"
    },
    {   "albumid": "7",
        "name":"Bad Meets Evil Eminem",
        "songs_count":"11"
    },
    {   "albumid": "8",
        "name":"Safe Trip Home",
        "songs_count":"11"
    },
    {   "albumid": "9",
        "name":"No Angel",
        "songs_count":"12"
    }
]

歌曲列表 JSON:

 [
  {
    "albumid": "1",
    "album": "127 Hours",
    "songs": [
      {
        "id": "1",
        "name": "Never Hear Surf Music Again",
        "duration": "5:52"
      },
      {
        "id": "2",
        "name": "The Canyon",
        "duration": "3:01"
      },
      {
        "id": "3",
        "name": "Liberation Begins",
        "duration": "2:14"
      },
      {
        "id": "4",
        "name": "Touch of the Sun",
        "duration": "4:39"
      },
      {
        "id": "5",
        "name": "Lovely Day",
        "duration": "4:16"
      },
      {
        "id": "6",
        "name": "Ca Plane Pour Moi",
        "duration": "3:00"
      },
      {
        "id": "7",
        "name": "Liberation In A Dream",
        "duration": "4:06"
      },
      {
        "id": "8",
        "name": "If You Love Me (Really Love Me)",
        "duration": "3:27"
      },
      {
        "id": "9",
        "name": "Acid Darbari",
        "duration": "4:21"
      },
      {
        "id": "10",
        "name": "R.I.P.",
        "duration": "5:11"
      },
      {
        "id": "11",
        "name": "Festival",
        "duration": "9:26"
      },
      {
        "id": "12",
        "name": "If I Rise",
        "duration": "4:38"
      },
      {
        "id": "13",
        "name": "Liberation",
        "duration": "3:11"
      },
      {
        "id": "14",
        "name": "Nocturne No. 2 in E flat",
        "duration": "4:01"
      }
    ]
  },
  {
    "albumid": "2",
    "album": "Adele 21",
    "songs": [
      {
        "id": "1",
        "name": "Rolling In The Deep",
        "duration": "03:48"
      },
      {
        "id": "2",
        "name": "Rumour Has It",
        "duration": "03:43"
      },
      {
        "id": "3",
        "name": "Turning Tables",
        "duration": "04:10"
      },
      {
        "id": "4",
        "name": "Don\u2019t You Remember",
        "duration": "04:03"
      },
      {
        "id": "5",
        "name": "Set Fire To The Rain",
        "duration": "04:02"
      },
      {
        "id": "6",
        "name": "He Won\u2019t Go",
        "duration": "04:38"
      },
      {
        "id": "7",
        "name": "Take It All",
        "duration": "03:48"
      },
      {
        "id": "8",
        "name": "I\u2019ll Be Waiting",
        "duration": "04:01"
      },
      {
        "id": "9",
        "name": "One And Only",
        "duration": "05:48"
      },
      {
        "id": "10",
        "name": "Lovesong",
        "duration": "05:16"
      },
      {
        "id": "11",
        "name": "Someone Like You",
        "duration": "04:45"
      }
    ]
  },
  {
    "albumid": "3",
    "album": "Lana Del Rey - Born to Die",
    "songs": [
      {
        "id": "1",
        "name": "Born to Die",
        "duration": "4:46"
      },
      {
        "id": "2",
        "name": "Off to the Races",
        "duration": "5:00"
      },
      {
        "id": "3",
        "name": "Blue Jeans",
        "duration": "3:29"
      },
      {
        "id": "4",
        "name": "Video Games",
        "duration": "4:42"
      },
      {
        "id": "5",
        "name": "Diet Mountain Dew",
        "duration": "3:43"
      },
      {
        "id": "6",
        "name": "National Anthem",
        "duration": "3:51"
      },
      {
        "id": "7",
        "name": "Dark Paradise",
        "duration": "4:03"
      },
      {
        "id": "8",
        "name": "Radio",
        "duration": "3:35"
      },
      {
        "id": 9,
        "name": "Carmen",
        "duration": "4:09"
      },
      {
        "id": "10",
        "name": "Million Dollar Man",
        "duration": "3:50"
      },
      {
        "id": "11",
        "name": "Summertime Sadness",
        "duration": "4:25"
      },
      {
        "id": "12",
        "name": "This Is What Makes Us Girls",
        "duration": "4:00"
      }
    ]
  },
  {
    "albumid": "4",
    "album": "Once",
    "songs": [
      {
        "id": "1",
        "name": "Falling Slowly",
        "duration": "4:05"
      },
      {
        "id": "2",
        "name": "If You Want Me",
        "duration": "3:47"
      },
      {
        "id": "3",
        "name": "Broken Hearted Hoover Fixer Sucker Guy",
        "duration": "0:52"
      },
      {
        "id": "4",
        "name": "When Your Mind's Made Up",
        "duration": "3:42"
      },
      {
        "id": "5",
        "name": "Lies",
        "duration": "3:58"
      },
      {
        "id": "6",
        "name": "Gold",
        "duration": "3:59"
      },
      {
        "id": "7",
        "name": "The Hill",
        "duration": "4:34"
      },
      {
        "id": "8",
        "name": "Fallen From The Sky",
        "duration": "3:24"
      },
      {
        "id": "9",
        "name": "Leave",
        "duration": "2:46"
      },
      {
        "id": "10",
        "name": "Trying To Pull Myself Away",
        "duration": "3:35"
      },
      {
        "id": "11",
        "name": "All The Way Down",
        "duration": "2:38"
      },
      {
        "id": "12",
        "name": "Once",
        "duration": "3:37"
      },
      {
        "id": "13",
        "name": "Say It To Me Now",
        "duration": "2:36"
      }
    ]
  },
  {
    "albumid": "5",
    "album": "Once",
    "songs": [
      {
        "id": "1",
        "name": "Falling Slowly",
        "duration": "4:05"
      },
      {
        "id": "2",
        "name": "If You Want Me",
        "duration": "3:47"
      },
      {
        "id": "3",
        "name": "Broken Hearted Hoover Fixer Sucker Guy",
        "duration": "0:52"
      },
      {
        "id": "4",
        "name": "When Your Mind's Made Up",
        "duration": "3:42"
      },
      {
        "id": "5",
        "name": "Lies",
        "duration": "3:58"
      },
      {
        "id": "6",
        "name": "Gold",
        "duration": "3:59"
      },
      {
        "id": "7",
        "name": "The Hill",
        "duration": "4:34"
      },
      {
        "id": "8",
        "name": "Fallen From The Sky",
        "duration": "3:24"
      },
      {
        "id": "9",
        "name": "Leave",
        "duration": "2:46"
      },
      {
        "id": "10",
        "name": "Trying To Pull Myself Away",
        "duration": "3:35"
      },
      {
        "id": "11",
        "name": "All The Way Down",
        "duration": "2:38"
      },
      {
        "id": "12",
        "name": "Once",
        "duration": "3:37"
      },
      {
        "id": "13",
        "name": "Say It To Me Now",
        "duration": "2:36"
      }
    ]
  },
  {
    "albumid": "6",
    "album": "Eminem Curtain Call",
    "songs": [
      {
        "id": "1",
        "name": "My Name Is",
        "duration": "4:28"
      },
      {
        "id": "2",
        "name": "The Way I Am",
        "duration": "4:51"
      },
      {
        "id": "3",
        "name": "Lose Yourself",
        "duration": "5:21"
      },
      {
        "id": "4",
        "name": "Shake That",
        "duration": "4:34"
      },
      {
        "id": "5",
        "name": "Sing For The Moment",
        "duration": "5:40"
      },
      {
        "id": "6",
        "name": "Without Me",
        "duration": "4:47"
      },
      {
        "id": "7",
        "name": "Like Toy Soldiers",
        "duration": "4:51"
      },
      {
        "id": "8",
        "name": "The Real Slim Shady",
        "duration": "4:44"
      },
      {
        "id": "9",
        "name": "Mockingbird",
        "duration": "4:11"
      },
      {
        "id": "10",
        "name": "Guilty Conscience",
        "duration": "3:19"
      },
      {
        "id": "11",
        "name": "Cleanin' Out My Closet",
        "duration": "4:58"
      },
      {
        "id": "12",
        "name": "Just Lose It",
        "duration": "4:08"
      },
      {
        "id": "13",
        "name": "When I'm Gone",
        "duration": "4:40"
      },
      {
        "id": "14",
        "name": "Stan",
        "duration": "6:21"
      }
    ]
  },
  {
    "albumid": "7",
    "album": "Bad Meets Evil Eminem",
    "songs": [
      {
        "id": "1",
        "name": "Lighters",
        "duration": "5:21"
      },
      {
        "id": "2",
        "name": "Fast Lane",
        "duration": "3:19"
      },
      {
        "id": "3",
        "name": "Above the Law",
        "duration": "6:21"
      },
      {
        "id": "4",
        "name": "Welcome 2 Hell",
        "duration": "4:34"
      },
      {
        "id": "5",
        "name": "Take from Me",
        "duration": "4:51"
      },
      {
        "id": "6",
        "name": "The Reunion",
        "duration": "4:44"
      },
      {
        "id": "7",
        "name": "A Kiss",
        "duration": "3:19"
      },
      {
        "id": "8",
        "name": "Echo",
        "duration": "6:21"
      },
      {
        "id": "9",
        "name": "Lighters",
        "duration": "4:40"
      },
      {
        "id": "10",
        "name": "Living Proof",
        "duration": "3:49"
      },
      {
        "id": "11",
        "name": "Loud Noises",
        "duration": "3:19"
      }
    ]
  },
  {
    "albumid": "8",
    "album": "Safe Trip Home",
    "songs": [
      {
        "id": "1",
        "name": "Don't Believe in Love",
        "duration": "3:52"
      },
      {
        "id": "2",
        "name": "Quiet Times",
        "duration": "3:16"
      },
      {
        "id": "3",
        "name": "Never Want to Say It's Love",
        "duration": "3:34"
      },
      {
        "id": "4",
        "name": "Grafton Street",
        "duration": "5:56"
      },
      {
        "id": "5",
        "name": "It Comes And It Goes",
        "duration": "3:26"
      },
      {
        "id": "6",
        "name": "Look No Further",
        "duration": "3:15"
      },
      {
        "id": "7",
        "name": "Us 2 Little Gods",
        "duration": "4:48"
      },
      {
        "id": "8",
        "name": "The Day Before The Day",
        "duration": "4:12"
      },
      {
        "id": "9",
        "name": "Let's Do the Things We Normally Do",
        "duration": "4:08"
      },
      {
        "id": "10",
        "name": "Burnin Love",
        "duration": "4:10"
      },
      {
        "id": "11",
        "name": "Northern Skies",
        "duration": "8:53"
      }
    ]
  },
  {
    "albumid": "9",
    "album": "No Angel",
    "songs": [
      {
        "id": "1",
        "name": "Here With Me",
        "duration": "4:05"
      },
      {
        "id": "2",
        "name": "Hunter",
        "duration": "3:55"
      },
      {
        "id": "3",
        "name": "Don't Think Of Me",
        "duration": "4:32"
      },
      {
        "id": "4",
        "name": "My Lover's Gone",
        "duration": "4:27"
      },
      {
        "id": "5",
        "name": "All You Want",
        "duration": "3:53"
      },
      {
        "id": "6",
        "name": "Thank You",
        "duration": "3:36"
      },
      {
        "id": "7",
        "name": "Honestly Ok",
        "duration": "4:37"
      },
      {
        "id": "8",
        "name": "Slide",
        "duration": "4:50"
      },
      {
        "id": "9",
        "name": "Isobel",
        "duration": "3:55"
      },
      {
        "id": "10",
        "name": "I'm No Angel",
        "duration": "3:55"
      },
      {
        "id": "11",
        "name": "My Life",
        "duration": "2:59"
      },
      {
        "id": "12",
        "name": "Take My Hand",
        "duration": "6:50"
      }
    ]
  }
]

AlbumsActivity.java:

protected String doInBackground(String... args) {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();

                // getting JSON string from URL
                String json = jsonParser.makeHttpRequest(URL_ALBUMS, "GET",
                        params);

                // Check your log cat for JSON reponse
                Log.d("Albums JSON: ", "> " + json);

                try {               
                    albums = new JSONArray(json);

                    if (albums != null) {
                        // looping through All albums
                        for (int i = 0; i < albums.length(); i++) {
                            JSONObject c = albums.getJSONObject(i);

                            // Storing each json item values in variable
                            String id = c.getString(TAG_ID);
                            String name = c.getString(TAG_NAME);
                            String songs_count = c.getString(TAG_SONGS_COUNT);

                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();

                            // adding each child node to HashMap key => value
                            map.put(TAG_ID, id);
                            map.put(TAG_NAME, name);
                            map.put(TAG_SONGS_COUNT, songs_count);

                            // adding HashList to ArrayList
                            albumsList.add(map);
                        }
                    }else{
                        Log.d("Albums: ", "null");
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }

TrackListActivity.java:

        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            // post album id as GET parameter
            params.add(new BasicNameValuePair(TAG_ALBUM_ID, album_id));

            // getting JSON string from URL
            String json = jsonParser.makeHttpRequest(URL_ALBUMS, "GET",
                    params);

            // Check your log cat for JSON reponse
            Log.d("Track List JSON: ", json);

            try {
                albumtracks = new JSONArray(json);

                if (albumtracks != null) {
                    for (int i = 0; i < albumtracks.length(); i++) {
                    JSONObject jObj = albumtracks.getJSONObject(i);
                    String album_id = jObj.getString(TAG_ALBUM_ID);
                    album_name = jObj.getString(TAG_ALBUM);
                    albums = jObj.getJSONArray(TAG_SONGS);

                    if (albums != null) {
                        // looping through All songs
                        for (int j = 0; j < albums.length(); j++) {
                            JSONObject c = albums.getJSONObject(j);

                            // Storing each json item in variable
                            String song_id = c.getString(TAG_SONG_ID);
                            // track no - increment i value
                            String track_no = String.valueOf(j + 1);
                            String name = c.getString(TAG_NAME);
                            String duration = c.getString(TAG_DURATION);

                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();

                            // adding each child node to HashMap key => value
                            map.put(TAG_ALBUM_ID, album_id);
                            map.put(TAG_SONG_ID, song_id);
                            map.put("track_no", track_no + ".");
                            map.put(TAG_NAME, name);
                            map.put(TAG_DURATION, duration);

                            // adding HashList to ArrayList
                            tracksList.add(map);
                        }
                    } else {
                        Log.d("Albums: ", "null");
                    }
                }

            }
                } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;

        }   

仅供引用,我希望不用 PHP 也能做到这一点。 预先感谢您!

最佳答案

说实话,看看你的代码,我认为构建应用程序的最佳方法是拥有一个结构,而不是使用一些不同的 map :

public AlbumListing {

    private String albumTitle
    private String albumArtist
    private HashMap<String, String> trackList = new HashMap<String, String>();

}

然后它有 getter 和 setter 来创建你的相册。然后,您可以拥有 AlbumListingArrayList:

ArrayList<AlbumListing> albumList = new ArrayList<AlbumListing>();

当您解析较高级别的信息(专辑标题和艺术家)时,首次创建新条目,当您解析较低级别的信息时,您可以搜索正确的艺术家条目,然后添加歌曲到条目的“trackList”中的私有(private) HashMap

像这样存储 JSON 结果会将一张专辑的所有数据保存在一起,而不必筛选两个 HashMap 来查找您需要的歌曲。我有一种感觉,如果您像这样构建数据,那么显示数据时遇到的问题就会消失,因为您将能够准确地看到您想要的数据所在的位置。

关于java - 无法解析复杂的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18734799/

相关文章:

java - 将十六进制编码/解码为 utf-8 字符串

java - 更新 SQLite Android 时出错

android - EditText 安卓 :hint not disappearing onFocus

android - 多次使用相同的错误代码调用 onError 的 RecognitionListener 回调

ios - NSString 到 JSON 字符串

javascript - 从 JQuery 动态选择并填充输入值

php - 如何在 mysql laravel 中存储 JSON

java - setActionCommand 设置的 Action 命令是什么?

java - EmbeddedDatabaseBuilder & org.h2.jdbc.JdbcSQLException : Wrong user name or password

java - jaxb - 如何从多态类创建 XML