先创建一个实体类。
public class User {
private String name;
private Integer age;
private String address;
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
一、FastJson的转换
1、对象转json
public void beanToJosnString() {
User user = new User();
user.setName("Json");
user.setAge(18);
user.setAddress("杭州");
User user1 = new User();
user1.setName("Java");
user1.setAge(19);
user1.setAddress("上海");
List<User> userList = new ArrayList<>();
userList.add(user);
userList.add(user1);
//java对象转json字符串
String json = JSONObject.toJSONString(user);
//java对象数组转json字符串
String jsonArray = JSONArray.toJSONString(userList);
System.out.println("json: "+json);
System.out.println("jsonArray: "+jsonArray);
}
输出结果:
json: {"address":"杭州","age":18,"name":"Json"}
jsonArray: [{"address":"杭州","age":18,"name":"Json"},{"address":"上海","age":19,"name":"Java"}]
2、json字符串转java对象
public void beanToJosnString() {
User user = new User();
user.setName("Json");
user.setAge(18);
user.setAddress("杭州");
User user1 = new User();
user1.setName("Java");
user1.setAge(19);
user1.setAddress("上海");
List<User> userList = new ArrayList<>();
userList.add(user);
userList.add(user1);
//java对象转json字符串
String json = JSONObject.toJSONString(user);
//java对象数组转json字符串
String jsonArray = JSONArray.toJSONString(userList);
User user2 = JSON.parseObject(json, User.class);
System.out.println(user2.getName()+" "+user2.getAge()+" "+user2.getAddress());
System.out.println("----------------------------");
List<User> list = JSON.parseArray(jsonArray, User.class);
for (User user3:list) {
System.out.println(user3.getName()+" "+user3.getAddress());
}
}
输出内容:
Json 18 杭州
--------------
Json 杭州
Java 上海
二、Json-Lib
1、java对象转成json字符串
public void beanToJosnString() {
User user = new User();
user.setName("Json");
user.setAge(18);
user.setAddress("杭州");
User user1 = new User();
user1.setName("Java");
user1.setAge(19);
user1.setAddress("上海");
List<User> userList = new ArrayList<>();
userList.add(user);
userList.add(user1);
//java对象转json字符串
String json = JSONObject.fromObject(user).toString();
//java对象数组转json字符串
String jsonArray = JSONArray.fromObject(userList).toString();
System.out.println("json: "+json);
System.out.println("jsonArray: "+jsonArray);
}
输出内容:
json: {"address":"杭州","age":18,"name":"Json"}
jsonArray: [{"address":"杭州","age":18,"name":"Json"},{"address":"上海","age":19,"name":"Java"}]
2、json字符串转java对象
public void beanToJosnString() {
User user = new User();
user.setName("Json");
user.setAge(18);
user.setAddress("杭州");
User user1 = new User();
user1.setName("Java");
user1.setAge(19);
user1.setAddress("上海");
List<User> userList = new ArrayList<>();
userList.add(user);
userList.add(user1);
//java对象转json字符串
String json = JSONObject.fromObject(user).toString();
//java对象数组转json字符串
String jsonArray = JSONArray.fromObject(userList).toString();
//json字符串转java对象
User userBean = (User) JSONObject.toBean(JSONObject.fromObject(json),User.class);
System.out.println(userBean.getName()+" "+userBean.getAddress());
System.out.println("--------------------");
//json字符串转java对象数组
List<User> list = (List<User>) JSONArray.toList(JSONArray.fromObject(jsonArray),User.class);
for (User user2:list) {
System.out.println(user2.getName()+" "+user2.getAge()+" "+user2.getAddress());
}
}
输出内容:
Json 杭州
--------------
Json 18 杭州
Java 19 上海