Java 语言 ArrayList 和 JSONArray 相互转换
2020-12-13 05:36
标签:== val main vat util stun sys lis this 本篇主要讲解 ArrayList 和 fastjson 中的 JSONArray相互转换的几种常用方式,以实体类 Student.java 举例说明。 简单总结了 6 种 方法(推荐 第5、6种),代码如下: 执行结果如下: 简单总结了 7 种 方法(推荐前 4种),代码如下: 执行结果如下: Java 语言 ArrayList 和 JSONArray 相互转换 标签:== val main vat util stun sys lis this 原文地址:https://www.cnblogs.com/miracle-luna/p/11143702.htmlJava 语言 ArrayList 和 JSONArray 相互转换
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
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
=============== 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
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
=============== 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‘}
文章标题:Java 语言 ArrayList 和 JSONArray 相互转换
文章链接:http://soscw.com/essay/31319.html