Java json转换实体类(JavaBean),实体类(JavaBean)转换json
JSON转换实体类
1. 使用Jackson库
public class User {
private String name;
private int age;
// getters and setters
}
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonDemo {
public static void main(String[] args) {
String json = "{\"name\":\"John\", \"age\":30}";
ObjectMapper mapper = new ObjectMapper();
try {
User user = mapper.readValue(json, User.class);
System.out.println(user.getName());
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. 使用Gson库
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
public class User {
private String name;
private int age;
// getters and setters
}
import com.google.gson.Gson;
public class JsonDemo {
public static void main(String[] args) {
String json = "{\"name\":\"John\", \"age\":30}";
Gson gson = new Gson();
User user = gson.fromJson(json, User.class);
System.out.println(user.getName());
}
}
3. 使用Fastjson库
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.15</version>
</dependency>
public class User {
private String name;
private int age;
// getters and setters
}
import com.alibaba.fastjson.JSON;
public class JsonDemo {
public static void main(String[] args) {
String json = "{\"name\":\"John\", \"age\":30}";
User user = JSON.parseObject(json, User.class);
System.out.println(user.getName());
}
}
4.在线工具
此外,还有一些在线工具可以帮助你将JSON转换为Java实体类,例如:
- JSON中文网的在线JSON转JavaBean工具https://www.json.cn/json2java/
这些工具和库可以帮助开发者更方便地将JSON数据转换为Java对象,从而简化数据处理过程。
实体类转换JSON
1. 使用Jackson库
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
public class User {
private String name;
private int age;
// getters and setters
}
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonDemo {
public static void main(String[] args) {
User user = new User();
user.setName("John");
user.setAge(30);
ObjectMapper mapper = new ObjectMapper();
try {
String json = mapper.writeValueAsString(user);
System.out.println(json);
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. 使用Gson库
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
public class User {
private String name;
private int age;
// getters and setters
}
import com.google.gson.Gson;
public class JsonDemo {
public static void main(String[] args) {
User user = new User();
user.setName("John");
user.setAge(30);
Gson gson = new Gson();
String json = gson.toJson(user);
System.out.println(json);
}
}
3. 使用Fastjson库
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.15</version>
</dependency>
public class User {
private String name;
private int age;
// getters and setters
}
import com.alibaba.fastjson.JSON;
public class JsonDemo {
public static void main(String[] args) {
User user = new User();
user.setName("John");
user.setAge(30);
String json = JSON.toJSONString(user);
System.out.println(json);
}
}
4.在线工具
此外,还有一些在线工具可以帮助你将Java实体类转换为JSON,例如:
- JSON中文网的在线JavaBean转JSON工具https://www.json.cn/json2java/
这些工具和库可以帮助开发者更方便地将Java对象转换为JSON字符串,从而简化数据传输和存储过程。