android初學------json字串陣列轉物件 儲存在list集合中 Gson
阿新 • • 發佈:2019-02-15
Gson gson=new Gson();
List<物件> vol = gson.fromJson(jsonStr, new TypeToken<List<物件>>(){}.getType());
如果json陣列中 含有比如list 的集合元素 也可以用這種方法
public class A{
private int id;
private String title;
private String photo;
private List<B> list;}
json格式 [public class B{ 這個類中的欄位屬性 是類A中List中存在的屬性欄位 private int id; private String title; private int size; private String photo; private String headerImg
[ { "id": "5", "title": "強烈推薦", "photo": "", "list": [ { "id": "6", "title": "aaa", "size": "1", "photo": "/files/2014-03-19/489284939.jpg", "headerImg": "/files/2014-03-19/972303602.jpg"}, { "id": "8", "title": "bbb", "size": "0", "photo": "/files/2014-03-19/153049045.jpg", }