比如我们有一个实体类:
public class Person { private String name; private int age;
public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
得到一个ArrayList
Person person = new Person();
person.setName("a");
person.setAge(1);
Person person1 = new Person();
person1.setName("b");
person1.setAge(2);
List<Person> personList = new ArrayList<>();
personList.add(person);
personList.add(person1);
ArrayList 转 JSONArray:
JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(personList));
JSONArray 转 String:
String str = jsonArray.toJSONString();
String 转 JSONArray:
JSONArray jsonArray1 = JSONArray.parseArray(str);
JSONArray 转 ArrayList:
List<Person> list = JSONObject.parseArray(jsonArray1.toJSONString(), Person.class);