private static String json = null;
private static String jsonList = null;
private static List<Student> studentList = null;
// Arrays.asList 的使用
public static void testArraylist(){
Student student = new Student();
studentList = Arrays.asList(new Student("jiminmin",20),new Student("jihuawei",30));
System.err.println("-----"+studentList.size());
}
// 对象转json串,json串转Student对象
public static void testStudentToJson(){
Student student = new Student("jiminmin",20);
Object obj = JSONObject.toJSON(student);
json = obj.toString(); // 转成json串
Student student = JSONObject.parseObject(json,Student.class);
System.out.println("----"+student.getName());
}
// json串转list集合
public static void testJsonListToStudentList(){
Object obj = JSONObject.toJSON(studentList);
jsonList = obj.toString(); // 转成json串
List<Student> list = JSONObject.parseArray(jsonList, Student.class);
System.out.println("====="+list.size());
}
// json串转jsonObject,读取其中的值
public static void testJsonObject(){
Student student = new Student("jiminmin",20);
String str= JSONArray.JSONObject(student).toString;
JSONObject json=JSONObject.parseObject(str);
String name= jsonObject.getString("name");
System.out.println("学生名称====="+name);
}
java 对象、list和json串之间的转换
最新推荐文章于 2024-04-18 05:39:52 发布