将 Java pojo 转换为 json 字符串

Lil*_*lac 9 java json objectmapper

我有以下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
Run Code Online (Sandbox Code Playgroud)

对于每个列表,我都有实体类,例如 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;
    }
Run Code Online (Sandbox Code Playgroud)

无论如何我可以在浏览器上显示我的json内容,如果是的话,我该怎么做?谢谢。

imp*_*ble 12

有 2 个库使用 Java 处理 JSON 序列化/反序列化:

  1. 杰克逊

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

    依赖项(databind 是主要依赖项,对于注释和附加功能,您将需要更多 Jackson 依赖项):

    行家:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>   
    </dependency>
    
    Run Code Online (Sandbox Code Playgroud)

    摇篮:

    dependencies {
       implementation "com.fasterxml.jackson.core:jackson-databind:${yourVersion}"
    }
    
    Run Code Online (Sandbox Code Playgroud)

    序列化片段:

    dependencies {
       implementation "com.fasterxml.jackson.core:jackson-databind:${yourVersion}"
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 格森

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

    依赖项:

    摇篮:

    dependencies { 
        implementation "com.google.code.gson:gson:${yourVersion}"
    }
    
    Run Code Online (Sandbox Code Playgroud)

    行家:

    TabularDescriptor tabularDescriptor = new TabularDescriptor();
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(tabularDescriptor);
    
    Run Code Online (Sandbox Code Playgroud)

    序列化片段:

    dependencies { 
        implementation "com.google.code.gson:gson:${yourVersion}"
    }
    
    Run Code Online (Sandbox Code Playgroud)

值得注意的细节:您必须将所有 getter/setter 公开才能实现对象的完整序列化和完整反序列化(以最简单的形式)。在任何情况下,空构造函数都是必须的。

参考信息:

  1. Java 中的 JSON 作者:Baeldung
  2. 杰克逊 vs Gson by Baeldung


Vla*_*can 6

我建议您将 Jackson 添加到您的项目中,它相当容易使用。

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

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

ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(tabularDescriptor);
TabularDescriptor newTabularDescriptor = objectMapper.readValue(json, TabularDescriptor.class);
Run Code Online (Sandbox Code Playgroud)