java Gson轉為list集合
一、創建相應實體:
public class ExpressStatusVO {
private String ftime;
private String context;// 信息內容
private String time;
public String getFtime() {
return ftime;
}
public void setFtime(String ftime) {
this.ftime = ftime;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
二、獲取Gson對象並進行轉換:
public Object getExpressDetails( ){
String strByJson = "[{"ftime":"2017-11-15 22:03:32","context":"客戶 簽收人: 牛春平 已簽收 感謝使用圓通速遞,期待再次為您服務","time":"2017-11-15 22:03:32"},
{"ftime":"2017-11-15 16:28:38","context":"北京市昌平區北七家公司(點擊查詢電話)居** 派件中 派件員電話15501296544","time":"2017-11-15 16:28:38"},
{"ftime":"2017-11-15 05:52:22","context":"北京市昌平區北七家公司 已收入","time":"2017-11-15 05:52:22"},
{"ftime":"2017-11-15 01:36:57","context":"北京轉運中心 已發出,下一站 北京市昌平區北七家","time":"2017-11-15 01:36:57"}]";
String strByJson = expressStatus.getData();
// Json to list
ExpressStatusVO[] array = new Gson().fromJson(strByJson, ExpressStatusVO[].class);
List<ExpressStatusVO> listVO = Arrays.asList(array);
Map<String, Object> map = new HashMap<String, Object>();
map.put("list", listVO);
return map;
}
返還的對象為:
"list": [ { "ftime": "2017-11-15 22:03:32", "context": "客戶 簽收人: 牛春平 已簽收 感謝使用圓通速遞,期待再次為您服務", "time": "2017-11-15 22:03:32" }, { "ftime": "2017-11-15 16:28:38", "context": "北京市昌平區北七家公司(點擊查詢電話)居** 派件中 派件員電話15501296544", "time": "2017-11-15 16:28:38" }, { "ftime": "2017-11-15 05:52:22", "context": "北京市昌平區北七家公司 已收入", "time": "2017-11-15 05:52:22" }, { "ftime": "2017-11-15 01:36:57", "context": "北京轉運中心 已發出,下一站 北京市昌平區北七家", "time": "2017-11-15 01:36:57" }]
註意:前臺需要的是list集合時,需將其轉為Map對象進行返回。
java Gson轉為list集合