2017-05-05 197 views
-2

我想在android中解析嵌套的json數組。我不知道如何解析數組中的數組。
下面是我的JSON代碼
在android中解析json嵌套數組

{ 
"album": [{ 
    "id_album": "svt001", 
    "name_album": "first album", 
    "song": [{ 
     "id_song": "svt0000001", 
     "track": "1" 
    }, { 
     "id_song": "svt0000002", 
     "track": "2" 
    }] 
}, { 
    "id_album": "svt002", 
    "name_album": "second album", 
    "song": [{ 
     "id_song": "svt0000003", 
     "track": "1" 
    }, { 
     "id_song": "svt0000004", 
     "track": "2" 
    }, { 
     "id_song": "svt0000005", 
     "track": "3" 
    }] 
}]} 

我可以分析id_album和name_album,但我不知道怎麼去id_song和跟蹤
下面是我Album.java

public class Album extends AppCompatActivity{ 
private String TAG = Album.class.getSimpleName(); 
private ListView lv; 

ArrayList<HashMap<String, String>> albumList; 

@Override 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.album); 

    albumList = new ArrayList<>(); 
    lv = (ListView) findViewById(R.id.list); 

    new GetAlbums().execute(); 
} 

private class GetAlbums extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     Toast.makeText(Album.this, "Album data is downloading", 
       Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     HttpHandler sh = new HttpHandler(); 

     String url = "http://lyric.noviaprims.tk/json/listalbum.php"; 
     String jsonStr = sh.makeServiceCall(url); 

     Log.e(TAG, "Response from url: " + jsonStr); 
     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 
       JSONArray album = jsonObj.getJSONArray("album"); 

       for (int i = 0; i < album.length(); i++) { 
        JSONObject c = song.getJSONObject(i); 
        String id_album = c.getString("id_album"); 
        String name_album = c.getString("name_album"); 

        HashMap<String, String> albums = new HashMap<>(); 

        albums.put("id_album", id_album); 
        albums.put("name_album", name_album); 

        albumList.add(albums); 
       } 
      } catch (final JSONException e) { 
       Log.e(TAG, "Json parsing error: " + e.getMessage()); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Toast.makeText(getApplicationContext(), 
           "Json parsing error: " + e.getMessage(), 
           Toast.LENGTH_LONG).show(); 
        } 
       }); 
      } 
     } else { 
      Log.e(TAG, "Couldn't get song from server"); 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        Toast.makeText(getApplicationContext(), 
          "Couldn't get album from server. Check logcat for possible errors", 
          Toast.LENGTH_LONG).show(); 
       } 
      }); 
     } 
     return null; 
    } 
}} 

回答

0

試試這個:

在你的build.gradle compile com.google.code.gson:gson:2.7'

在你的包添加此gradle這個並添加以下類

import java.util.List; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class Example { 

    @SerializedName("album") 
    @Expose 
    private List<Album> album = null; 

    public List<Album> getAlbum() { 
     return album; 
    } 

    public void setAlbum(List<Album> album) { 
     this.album = album; 
    } 

    public class Song { 

     @SerializedName("id_song") 
     @Expose 
     private String idSong; 
     @SerializedName("track") 
     @Expose 
     private String track; 

     public String getIdSong() { 
      return idSong; 
     } 

     public void setIdSong(String idSong) { 
      this.idSong = idSong; 
     } 

     public String getTrack() { 
      return track; 
     } 

     public void setTrack(String track) { 
      this.track = track; 
     } 

    } 
    public class Album { 

     @SerializedName("id_album") 
     @Expose 
     private String idAlbum; 
     @SerializedName("name_album") 
     @Expose 
     private String nameAlbum; 
     @SerializedName("song") 
     @Expose 
     private List<Song> song = null; 

     public String getIdAlbum() { 
      return idAlbum; 
     } 

     public void setIdAlbum(String idAlbum) { 
      this.idAlbum = idAlbum; 
     } 

     public String getNameAlbum() { 
      return nameAlbum; 
     } 

     public void setNameAlbum(String nameAlbum) { 
      this.nameAlbum = nameAlbum; 
     } 

     public List<Song> getSong() { 
      return song; 
     } 

     public void setSong(List<Song> song) { 
      this.song = song; 
     } 

    } 
} 

然後用

Example example = new Gson().fromJson(jsonStr,Example.class); 
List<Album> albums = example.getAlbum(); 
List<Song> songs = albums.get(0).getSong(); 

,並使用this鏈接,創建POJO並在此url選擇JSON和GSON選項

0

嘗試以下代碼:

public class Album extends AppCompatActivity{ 
private String TAG = Album.class.getSimpleName(); 
private ListView lv; 

ArrayList<HashMap<String, String>> albumList; 

@Override 
protected void onCreate(Bundle savedInstanceState){ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.album); 

albumList = new ArrayList<>(); 
lv = (ListView) findViewById(R.id.list); 

new GetAlbums().execute(); 
} 

private class GetAlbums extends AsyncTask<Void, Void, Void> { 
@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    Toast.makeText(Album.this, "Album data is downloading", 
      Toast.LENGTH_LONG).show(); 
} 

@Override 
protected Void doInBackground(Void... arg0) { 
    HttpHandler sh = new HttpHandler(); 

    String url = "http://lyric.noviaprims.tk/json/listalbum.php"; 
    String jsonStr = sh.makeServiceCall(url); 

    Log.e(TAG, "Response from url: " + jsonStr); 
    if (jsonStr != null) { 
     try { 
      JSONObject jsonObj = new JSONObject(jsonStr); 
      JSONArray album = jsonObj.getJSONArray("album"); 

      for (int i = 0; i < album.length(); i++) { 
       JSONObject c = song.getJSONObject(i); 
       String id_album = c.getString("id_album"); 
       String name_album = c.getString("name_album"); 

       JSONArray song = c.getJSONArray("song"); 
       for(int k=0; k<song.length(); k++){ 
        JSONObject songObject = song.getJSONObject(k); 
        String id_song = songObject.getString("id_song"); 
        String track = songObject.getString("track"); 
       } 

       HashMap<String, String> albums = new HashMap<>(); 

       albums.put("id_album", id_album); 
       albums.put("name_album", name_album); 

       albumList.add(albums); 
      } 
     } catch (final JSONException e) { 
      Log.e(TAG, "Json parsing error: " + e.getMessage()); 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        Toast.makeText(getApplicationContext(), 
          "Json parsing error: " + e.getMessage(), 
          Toast.LENGTH_LONG).show(); 
       } 
      }); 
     } 
    } else { 
     Log.e(TAG, "Couldn't get song from server"); 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       Toast.makeText(getApplicationContext(), 
         "Couldn't get album from server. Check logcat for possible errors", 
         Toast.LENGTH_LONG).show(); 
      } 
     }); 
    } 
    return null; 
} 
}} 
0

1)您應該格式化您的Json以獲得更好的可讀性ty,例如與http://www.jsoneditoronline.org比你

{ 
    "album": [ 
    { 
     "id_album": "svt001", 
     "name_album": "first album", 
     "song": [ 
     { 
      "id_song": "svt0000001", 
      "track": "1" 
     }, 
     { 
      "id_song": "svt0000002", 
      "track": "2" 
     } 
     ] 
    }, 
    { 
     "id_album": "svt002", 
     "name_album": "second album", 
     "song": [ 
     { 
      "id_song": "svt0000003", 
      "track": "1" 
     }, 
     { 
      "id_song": "svt0000004", 
      "track": "2" 
     }, 
     { 
      "id_song": "svt0000005", 
      "track": "3" 
     } 
     ] 
    } 
    ] 
} 

2)你可以用這個插件https://plugins.jetbrains.com/plugin/7654-gsonformat

3)使用RetrofitMoshi創建你的POJO下載數據