一下都要引入:
import com.alibaba.fastjson.JSON;
1.List对象转字符串
List<User> userList = new ArrayList<User>(); //userList 可以自己拿,这里就取一个 User user = new User(); user.setName("aaa"); userList.add(user); String jsonString = JSON.toJSONString(userList);
System.out.println("jsonString:" + jsonString);
2.字符串转List
List<User> users = JSON.parseArray(jsonString, User.class); System.out.println("users:" + users);