将Java pojo转换为json字符串

20

我有以下的Java类:

public  class TabularDescriptor extends ReportDescriptor {

    private String generatorClass;
    private String targetClass;
    private String name;
    private String sublabel;
    private String reportName;
    private List<MappingMetadata> mappings = null;
    private List<TabularColumnGroup> columnGroups = null;
    private List<TabularStates> states = null;
:
:
     and its getters and settere

我有一些实体类,分别用于MappingMetadata、TabularColumnGroup和TabularStates这几个列表。我想为这些POJO类获取JSON数据。该怎么做呢?

还有,这是什么用处呢?

    public JSONObject toJSON() {
        JSONObject ret = new JSONObject();
        ret.put("generatorClass", this.generatorClass);
        ret.put("targetClass", this.targetClass);
        ret.put("name", this.name);
        :
        :
        return ret;
    }

如果可以,我该如何在浏览器上显示我的JSON内容?谢谢。


2
看一下 https://github.com/google/gson。它似乎正是你所需要的。你可以很容易地使用它来序列化和反序列化Java对象。 - Ian Rehwinkel
3个回答

12
有两个使用Java进行JSON序列化/反序列化的库:
  1. Jackson

    Java序列化/反序列化的首选库(文档)。对于大多数开发人员来说,它是Java中处理JSON交互的默认选择。在Spring Boot - 流行的Java IOC/DI框架的依赖启动器spring-boot-starter-webspring-boot-starter-webflux中,已经完全嵌入了所有依赖项。

    依赖项 (databind是主要依赖项,对于注解和其他功能,您需要更多的Jackson依赖项):

    Maven:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>   
    </dependency>
    

    Gradle:

    dependencies {
       implementation "com.fasterxml.jackson.core:jackson-databind:${yourVersion}"
    }
    

    序列化代码片段:

    TabularDescriptor tabularDescriptor = new TabularDescriptor();
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(tabularDescriptor);
    
  2. Gson

    Google的Java序列化/反序列化库(文档)。

    依赖项:

    Gradle:

    dependencies { 
        implementation "com.google.code.gson:gson:${yourVersion}"
    }
    

    Maven:

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>${gson.version}</version>
    </dependency>
    

    序列化代码片段:

    TabularDescriptor tabularDescriptor = new TabularDescriptor();
    Gson gson = new Gson();
    String json = gson.toJson(obj);
    
值得注意的细节:为了完全序列化和完全反序列化一个对象(在其最简单的形式下),你必须将所有的getter/setter都设置为公共的。无论如何,一个空的构造函数是必须的。
参考信息:
  1. Baeldung的Java中的JSON
  2. Baeldung的Jackson vs Gson

6
我建议您将Jackson添加到您的项目中,这很容易使用。
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>

而在Java中,代码可以这样使用:

ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(tabularDescriptor);
TabularDescriptor newTabularDescriptor = objectMapper.readValue(json, TabularDescriptor.class);

谢谢。tabularDescriptor中的value是什么?它是我使用setter设置的值吗?如果是,那么我如何将值设置为像MappingMetadata这样的pojo类? - Lilac
1
@Lilac 1) tabularDescriptor 是你的对象,例如 TabularDescriptor tabularDescriptor = new TabularDescriptor(...); 2) 我不确定你的意思。你是指如何创建对象吗?最简单的方法是使用构建器模式,特别是在你的情况下,因为你有很多内部对象在表格描述符中。 - Vladucu Voican
谢谢。我不确定如何为内部对象赋值。 - Lilac

4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接