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

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); // not go into List
		List<LiuJieTestObj> objList = JSON.parseArray(str, LiuJieTestObj.class);// 取一个List

		// Error 2 given Model: Thread Exception in "main" com.alibaba.fastjson.JSONException: Expect '[', {But, POS. 1, line1, column 2 
		String str2 = JSONArray.toJSONString (obj); // put in List of non-object 
		List <LiuJieTestObj> objList2 = JSON.parseArray ( str2, LiuJieTestObj.class); // get a List 

		// correct model. 3 
		List <LiuJieTestObj> = new new List the ArrayList <> (); 
		List.add (obj ); 
		String Str3 = JSON.toJSONString (List); // put in the List 
		List <LiuJieTestObj> objList3 = JSON.parseArray (Str3, LiuJieTestObj.class); // get a List 

	} 
}

  

 

Guess you like

Origin www.cnblogs.com/j-liu3323/p/11579611.html