Gson多层嵌套Json解析

Gson多层嵌套Json解析

Json数据


{
"mp3list":
[
    {
        "name": "爱我别走",
        "artist": "张震岳",
        "size": "2.8M",
        "url": "http://127.0.0.1"
    },
    {
        "name": "那年夏天",
        "artist": "许飞",
        "size": "2.8M",
        "url": "http://127.0.0.1"
    },
    {
        "name": "大城小爱",
        "artist": "王力宏",
        "size": "2.8M",
        "url": "http://127.0.0.1"
    }
]
}



构造解析类

采用静态内部类的形式实现多层嵌套
对于数组采用List成员实现



public class Mp3List {

public List mp3list;



public static class Mp3Info {

private String name;
private String artist;
private String size;
private String url;

}
   }


   解析数据
   
                Mp3List list = null;
 
InputStream is = getClassLoader().getResourceAsStream("musics.json");
       
       
      Gson gson = new Gson();
       
    list = gson.fromJson(new InputStreamReader(is),Mp3List.class);

你可能感兴趣的:(Gson多层嵌套Json解析)