Java 语言 ArrayList 和 JSONArray 相互转换
本篇主要讲解 ArrayList 和 fastjson 中的 JSONArray相互转换的几种常用方式,以实体类 Student.java 举例说明。
Student 实体类
/** * @author Miracle Luna * @version 1.0 * @date 2019/7/5 18:06 */ public class Student { private String name; // 姓名 private Integer age; // 年龄 private String gender; // 性别 public Student(String name, Integer age, String gender) { this.name = name; this.age = age; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ", gender=‘" + gender + ‘\‘‘ + ‘}‘; } }
ArrayList 转成 JSONArray
简单总结了 6 种 方法(推荐 第5、6种),代码如下:
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.ArrayList; /** * @author Miracle Luna * @version 1.0 * @date 2019/7/5 17:43 */ public class ArrayListToJSONArray { public static void main(String[] args) { ArrayList<Student> studentList = new ArrayList<Student>(); JSONArray studentJsonArray = new JSONArray(); Student John = new Student("John", 16,"boy"); Student Lily = new Student("Lily", 17, "girl"); Student Jack = new Student("Jack", 18, "boy"); studentList.add(John); studentList.add(Lily); studentList.add(Jack); System.out.println("=============== studentList info ================"); System.out.println(studentList.toString()); // 方式 1 studentJsonArray = JSON.parseArray(JSONObject.toJSONString(studentList)); System.out.println("\n方式 1: " + studentJsonArray.toJSONString()); // 方式 2 studentJsonArray = JSON.parseArray(JSON.toJSONString(studentList)); System.out.println("\n方式 2: " + studentJsonArray.toJSONString()); // 方式 3 studentJsonArray = JSONObject.parseArray(JSONObject.toJSONString(studentList)); System.out.println("\n方式 3: " + studentJsonArray.toJSONString()); // 方式 4 studentJsonArray = JSONObject.parseArray(JSON.toJSONString(studentList)); System.out.println("\n方式 4: " + studentJsonArray.toJSONString()); // 方式 5 studentJsonArray = JSONArray.parseArray(JSONObject.toJSONString(studentList)); System.out.println("\n方式 5: " + studentJsonArray.toJSONString()); // 方式 6 studentJsonArray = JSONArray.parseArray(JSON.toJSONString(studentList)); System.out.println("\n方式 6: " + studentJsonArray.toJSONString()); System.out.println("\n============== Lambda 表达式 遍历 JSONArray ============"); studentJsonArray.forEach(student -> System.out.println("student info: " + student)); } }
执行结果如下:
=============== studentList info ================ [Student{name=‘John‘, age=16, gender=‘boy‘}, Student{name=‘Lily‘, age=17, gender=‘girl‘}, Student{name=‘Jack‘, age=18, gender=‘boy‘}] 方式 1: [{"gender":"boy","name":"John","age":16},{"gender":"girl","name":"Lily","age":17},{"gender":"boy","name":"Jack","age":18}] 方式 2: [{"gender":"boy","name":"John","age":16},{"gender":"girl","name":"Lily","age":17},{"gender":"boy","name":"Jack","age":18}] 方式 3: [{"gender":"boy","name":"John","age":16},{"gender":"girl","name":"Lily","age":17},{"gender":"boy","name":"Jack","age":18}] 方式 4: [{"gender":"boy","name":"John","age":16},{"gender":"girl","name":"Lily","age":17},{"gender":"boy","name":"Jack","age":18}] 方式 5: [{"gender":"boy","name":"John","age":16},{"gender":"girl","name":"Lily","age":17},{"gender":"boy","name":"Jack","age":18}] 方式 6: [{"gender":"boy","name":"John","age":16},{"gender":"girl","name":"Lily","age":17},{"gender":"boy","name":"Jack","age":18}] ============== Lambda 表达式 遍历 JSONArray ============ student info: {"gender":"boy","name":"John","age":16} student info: {"gender":"girl","name":"Lily","age":17} student info: {"gender":"boy","name":"Jack","age":18}
JSONArray 转成 ArrayList
简单总结了 7 种 方法(推荐前 4种),代码如下:
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.*; /** * @author Miracle Luna * @version 1.0 * @date 2019/7/5 18:36 */ public class JSONArrayToArrayList { public static void main(String[] args) { JSONArray studentJSONArray = new JSONArray(); List<Student> studentList = new ArrayList<Student>(); Map<String, Object> JohnMap = new HashMap<String, Object>(); JohnMap.put("name", "John"); JohnMap.put("age", 16); JohnMap.put("gender", "boy"); JSONObject John = new JSONObject(JohnMap); Map<String, Object> LilyMap = new HashMap<String, Object>(); LilyMap.put("name", "Lily"); LilyMap.put("age", 17); LilyMap.put("gender", "girl"); JSONObject Lily = new JSONObject(LilyMap); Map<String, Object> JackMap = new HashMap<String, Object>(); JackMap.put("name", "Jack"); JackMap.put("age", 18); JackMap.put("gender", "boy"); JSONObject Jack = new JSONObject(JackMap); studentJSONArray.add(John); studentJSONArray.add(Lily); studentJSONArray.add(Jack); System.out.println("\n=============== studentJSONArray info ================"); System.out.println(studentJSONArray); System.out.println("\n=============== JSONArray to ArrayList(方式 1) ================"); studentList = studentJSONArray.toJavaList(Student.class); studentList.forEach(student -> System.out.println("stundet info: " + student)); System.out.println("\n=============== JSONArray to ArrayList(方式 2) ================"); studentList = JSON.parseArray(studentJSONArray.toJSONString(), Student.class); studentList.forEach(student -> System.out.println("stundet info: " + student)); System.out.println("\n=============== JSONArray to ArrayList(方式 3) ================"); studentList = JSONObject.parseArray(studentJSONArray.toJSONString(), Student.class); studentList.forEach(student -> System.out.println("stundet info: " + student)); System.out.println("\n=============== JSONArray to ArrayList(方式 4) ================"); studentList = JSONArray.parseArray(studentJSONArray.toJSONString(), Student.class); studentList.forEach(student -> System.out.println("stundet info: " + student)); System.out.println("\n=============== JSONArray to ArrayList(方式 5) ================"); final ArrayList<Student> tmpList = new ArrayList<Student>(); studentJSONArray.forEach(studentJson -> { JSONObject jsonObject = (JSONObject)studentJson; Student student = new Student(jsonObject.getString("name"), jsonObject.getInteger("age"), jsonObject.getString("gender")); tmpList.add(student); }); studentList = tmpList; studentList.forEach(student -> System.out.println("stundet info: " + student)); System.out.println("\n=============== JSONArray to ArrayList(方式 6) ================"); studentList.clear(); for (Object object : studentJSONArray) { JSONObject jsonObject = (JSONObject)object; Student student = new Student(jsonObject.getString("name"), jsonObject.getInteger("age"), jsonObject.getString("gender")); studentList.add(student); } studentList.forEach(student -> System.out.println("stundet info: " + student)); System.out.println("\n=============== JSONArray to ArrayList(方式 7) ================"); studentList.clear(); for (int i = 0; i < studentJSONArray.size(); i++) { JSONObject jsonObject = (JSONObject)studentJSONArray.get(i); Student student = new Student(jsonObject.getString("name"), jsonObject.getInteger("age"), jsonObject.getString("gender")); studentList.add(student); } studentList.forEach(student -> System.out.println("stundet info: " + student)); } }
执行结果如下:
=============== studentJSONArray info ================ [{"gender":"boy","name":"John","age":16},{"gender":"girl","name":"Lily","age":17},{"gender":"boy","name":"Jack","age":18}] =============== JSONArray to ArrayList(方式 1) ================ stundet info: Student{name=‘John‘, age=16, gender=‘boy‘} stundet info: Student{name=‘Lily‘, age=17, gender=‘girl‘} stundet info: Student{name=‘Jack‘, age=18, gender=‘boy‘} =============== JSONArray to ArrayList(方式 2) ================ stundet info: Student{name=‘John‘, age=16, gender=‘boy‘} stundet info: Student{name=‘Lily‘, age=17, gender=‘girl‘} stundet info: Student{name=‘Jack‘, age=18, gender=‘boy‘} =============== JSONArray to ArrayList(方式 3) ================ stundet info: Student{name=‘John‘, age=16, gender=‘boy‘} stundet info: Student{name=‘Lily‘, age=17, gender=‘girl‘} stundet info: Student{name=‘Jack‘, age=18, gender=‘boy‘} =============== JSONArray to ArrayList(方式 4) ================ stundet info: Student{name=‘John‘, age=16, gender=‘boy‘} stundet info: Student{name=‘Lily‘, age=17, gender=‘girl‘} stundet info: Student{name=‘Jack‘, age=18, gender=‘boy‘} =============== JSONArray to ArrayList(方式 5) ================ stundet info: Student{name=‘John‘, age=16, gender=‘boy‘} stundet info: Student{name=‘Lily‘, age=17, gender=‘girl‘} stundet info: Student{name=‘Jack‘, age=18, gender=‘boy‘} =============== JSONArray to ArrayList(方式 6) ================ stundet info: Student{name=‘John‘, age=16, gender=‘boy‘} stundet info: Student{name=‘Lily‘, age=17, gender=‘girl‘} stundet info: Student{name=‘Jack‘, age=18, gender=‘boy‘} =============== JSONArray to ArrayList(方式 7) ================ stundet info: Student{name=‘John‘, age=16, gender=‘boy‘} stundet info: Student{name=‘Lily‘, age=17, gender=‘girl‘} stundet info: Student{name=‘Jack‘, age=18, gender=‘boy‘}
原文地址:https://www.cnblogs.com/miracle-luna/p/11143702.html
时间: 2024-10-11 05:10:38