1. 程式人生 > >fastjson解析list ,object中含有list, object中含有map

fastjson解析list ,object中含有list, object中含有map

for st2 當前 arr throw port .class fastjson 獲取

1.首先定義測試vo

package com.haiyisoft.cAssistantWeb.ui;

import java.sql.Timestamp;

public class vo {
private Long id;
private Timestamp date;
public Timestamp getDate() {
return date;
}
public void setDate(Timestamp date) {
this.date = date;
}
private Long craeateopear;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}


public Long getCraeateopear() {
return craeateopear;
}
public void setCraeateopear(Long craeateopear) {
this.craeateopear = craeateopear;
}

}

2.進行測試

package com.haiyisoft.cAssistantWeb.ui;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.haiyisoft.cAssistantWeb.vo.ReturnValueVo;
import com.haiyisoft.cloud.web.util.CommonUtil;

public class test {
public static void main(String[] args) throws Exception {
Map<String, List<vo>> map = new HashMap<String, List<vo>>();
System.out.println("獲取當前時間:"+CommonUtil.getDate());
List<vo> list =new ArrayList<vo>();
for(int i=0;i<4;i++){
vo vo =new vo();
vo.setId(Long.valueOf("1"+i));
vo.setCraeateopear(Long.valueOf("2"+i));
vo.setDate(CommonUtil.getDate());
list.add(vo);
}
map.put("listmap", list);
ReturnValueVo result = JsonReturn.assemblyBean(list, StateEnum.SUCCESS.getStatus(),"成功");
ReturnValueVo resultmap = JsonReturn.assemblyBean(map, StateEnum.SUCCESS.getStatus(),"成功");
String JSONObject = JSON.toJSONString(result);//將object打成json串
System.out.println(JSONObject);
String JSONObjectList = JSON.toJSONString(list);//將list打成json串
String JSONObjectMap = JSON.toJSONString(resultmap);//將map打成json串
//=============================================================================解析object
ReturnValueVo parseObject = JSON.parseObject(JSONObject, ReturnValueVo.class);
Object list2= parseObject.getObj();//獲取object內容
String list2JSONObject = JSON.toJSONString(list2);//將object內容轉成json串
List<vo> list4= JSON.parseArray(list2JSONObject , vo.class);//再對list解析
System.out.println(list4.get(0).getDate());//獲取時間
//===========================================解析list
List<vo> list3= JSON.parseArray(JSONObjectList, vo.class);
Timestamp id = list3.get(0).getDate();
System.out.println(id);
//===========================================解析map
ReturnValueVo parseMap= JSON.parseObject(JSONObjectMap, ReturnValueVo.class);
Object map2= parseMap.getObj();//獲取object內容
String JSONMap2 = JSON.toJSONString(map2);//將map打成json串
Map maps = (Map)JSON.parse(JSONMap2);
Object maplist = maps.get("listmap");
String JSONlist2 = JSON.toJSONString(maplist);//將map打成json串
List<vo> listc= JSON.parseArray(JSONlist2 , vo.class);//再對list解析
Timestamp cc = listc.get(0).getDate();
System.out.println(cc);

}
}

fastjson解析list ,object中含有list, object中含有map