import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @description: 测试
* @author: 码上得天下
* @create: 2019-11-20 13:15
**/
public class JSONArrayTest {
@Test
public void test( ) {
List<Map> teacherMapList = new ArrayList<Map>();
Map teacherMap = new HashMap();
teacherMap.put("name","张老师");
teacherMap.put("age",12);
List<Map> studentMapList = new ArrayList<Map>();
Map studentMap1 = new HashMap();
studentMap1.put("name","方同学");
studentMap1.put("age",24);
Map studentMap2 = new HashMap();
studentMap2.put("name","赵同学");
studentMap2.put("age",36);
studentMapList.add(studentMap1);
studentMapList.add(studentMap2);
teacherMap.put("list",studentMapList);
teacherMapList.add(teacherMap);
Teacher teacher = new Teacher();
// Map转DTO
try {
BeanUtils.populate(teacher,teacherMap);
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
System.out.println("BeanUtils.populate:"+teacher);
// list转DTO
JSONArray resultJsonArray = (JSONArray) JSON.parseArray(JSON.toJSONString(teacherMapList));
List<Teacher> list = resultJsonArray.toJavaList(Teacher.class);
System.out.println("JSONArray:"+ list);
}
}