您好,登录后才能下订单哦!
这篇文章主要介绍“怎么将Java对象转换为JSON”,在日常操作中,相信很多人在怎么将Java对象转换为JSON问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么将Java对象转换为JSON”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
在此代码段中,我们执行以下操作:
使用 setter 方法创建Student
对象并设置其属性。
创建JSONObject
调用object
并将Student
对象用作其构造函数的参数。
JSONObject
使用 getter 方法生成 JSON 字符串。
调用object.toString()
方法获取 JSON 字符串。
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import org.json.JSONObject; import java.util.Arrays; public class PojoToJSON { public static void main(String[] args) throws JsonProcessingException { Student student = new Student(); student.setId(1L); student.setName("Alice"); student.setAge(20); student.setCourses(Arrays.asList("Engineering", "Finance", "Chemistry")); JSONObject object = new JSONObject(student); String json = object.toString(); System.out.println(json); System.out.println(new Gson().toJson(student)); // Creating Object of ObjectMapper define in Jackson API ObjectMapper Obj = new ObjectMapper(); // Converting the Java object into a JSON string String jsonStr = Obj.writeValueAsString(student); // Displaying Java object into a JSON string System.out.println(jsonStr); } }
运行此代码会产生以下结果:
{"courses":["Engineering","Finance","Chemistry"],"name":"Alice","id":1,"age":20}
{"id":1,"name":"Alice","age":20,"courses":["Engineering","Finance","Chemistry"]}
{"id":1,"name":"Alice","age":20,"courses":["Engineering","Finance","Chemistry"]}
上面代码中使用的Student类:
import java.util.List; public class Student { private Long id; private String name; private int age; private List<String> courses; public Student(Long id, String name, int age, List<String> courses) { this.id = id; this.name = name; this.age = age; this.courses = courses; } Student() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<String> getCourses() { return courses; } public void setCourses(List<String> courses) { this.courses = courses; } }
Maven 依赖项
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.javaobjectjson</groupId> <artifactId>JavaObjectJSON</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.json/json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20211205</version> </dependency> <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.1</version> </dependency> </dependencies> </project>
到此,关于“怎么将Java对象转换为JSON”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。