package test; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; public class VarTest { public static void main(String[] args) { LiuJieTestObj obj = new LiuJieTestObj(); obj.setId(111L); Date date1 = new Date(2019, 7, 23); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); df.format(date1); obj.setTestDate(date1); // 错误示范1 报错:Exception in thread "main" com.alibaba.fastjson.JSONException: expect '[', but {, pos 1, line1,column 2 String str = JSON.toJSONString(obj);// 放进去的不是List List<LiuJieTestObj> objList = JSON.parseArray(str, LiuJieTestObj.class);// 取一个List // 错误示范2 报错:Exception in thread "main" com.alibaba.fastjson.JSONException: expect '[', but {, pos 1, line1,column 2 String str2 = JSONArray.toJSONString(obj);// 放进去的是非List的对象 List<LiuJieTestObj> objList2 = JSON.parseArray(str2, LiuJieTestObj.class);// 取一个List // 正确示范3 List<LiuJieTestObj> list = new ArrayList<>(); list.add(obj); String str3 = JSON.toJSONString(list);// 放进去的是List List<LiuJieTestObj> objList3 = JSON.parseArray(str3, LiuJieTestObj.class);// 取一个List } }