Json和List互转化
使用fastjson
List转Json
List<Student> students = new ArrayList();
String str = JSON.toJSONString(students); // List转json
Json 转List 方法一
String json = ""; //获取的Json数据
List<Student> students = JSON.parseObject(json,new TypeReference<List<Student>>(){}); // Json 转List
Json 转List方法二
List<Student> students = JSON.parseArray(json,Student.class);
Student 对象要实现Serializable接口
import java.io.Serializable;
public class Student implements Serializable{ }
使用gson
List<Student> students = new ArrayList();
Gson gson = new Gson();
//Entity JavaBean --> Json字符串
String str = gson.toJson(Student);
//Json字符串 --> Entity JavaBean
Person person = gson.fromJson(str, Student.class);
//json -- list
List<NoteItem> allNotes = new Gson().fromJson(json,new TypeToken<List<NoteItem>>(){}.getType());
//Student 对象要实现Serializable接口