报错原因:你放的是一个非List的对象 却想取一个List对象出来

com.aliyun.openservices.shade.com.alibaba.fastjson.JSONException: exepct '[', but {, pos 1, line 1, column 2-LMLPHP

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 str1 = JSON.toJSONString(obj);// 放进去的不是List
System.out.println("str1:"+str1);//str1:{"id":111,"testDate":61524633600000}
List<LiuJieTestObj> objList1 = JSON.parseArray(str1, 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的对象
System.out.println("str2:"+str2);//str2:{"id":111,"testDate":61524633600000}
List<LiuJieTestObj> objList2 = JSON.parseArray(str2, LiuJieTestObj.class);// 取一个List // 正确示范3
List<LiuJieTestObj> list = new ArrayList<>();
list.add(obj);
String str3 = JSON.toJSONString(list);// 放进去的是List
System.out.println("str3:"+str3);//str3:[{"id":111,"testDate":61524633600000}]
List<LiuJieTestObj> objList3 = JSON.parseArray(str3, LiuJieTestObj.class);// 取一个List }
}
package test;

import java.util.Date;

public class LiuJieTestObj {
private Long id ;
private Date testDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getTestDate() {
return testDate;
}
public void setTestDate(Date testDate) {
this.testDate = testDate;
}
}
05-28 18:25