得到一个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);
二:bean、JSONObject、String之间的转化:
得到一个JAVA对象:
Person person = new Person();
person.setName("a");
person.setAge(1);
JAVA对象转JSON对象:
JSONObject json = (JSONObject) JSONObject.toJSON(person);
JSON对象转JSON字符串String:
String str = json.toJSONString();
JSON字符串String转JSON对象:
JSONObject json1 = JSONObject.parseObject(str);
JSON对象转JAVA对象:
Person person1 = JSONObject.toJavaObject(json1, Person.class);