gpt4 book ai didi

java - 如何为 JsonObjects 的 JsonObject 创建 Jackson XML POJO 类

转载 作者:行者123 更新时间:2023-11-30 01:55:09 27 4
gpt4 key购买 nike

我正在尝试为以下 JSON 结构创建 POJO。 Fields 节点很容易连接,但我不确定如何使用注释来连接 Description 节点。如果我为该节点定义了 JSON 结构,我会创建一个 JsonObjects 的 JsonArray,这将使 java 类变得简单,但由于我没有,所以我需要弄清楚如何序列化下面的结构:

{
"Fields": {
"Required": ["ftp.hostname"],
"Optional": ["ftp.rootDirectory"]
},
"Description": {
"ftp.hostname": {
"label": "SFTP Hostname",
"description": "SFTP server hostname or IP address"
},
"ftp.rootDirectory": {
"label": "Root Directory",
"description": "The root path on the Data Store accessible by this connector"
}
}
}

请注意,Description 对象中的节点具有与 Fields 节点中定义的值相关的名称,这意味着它们的节点名称可能因负载而异。

Fields 节点的类:

public class FieldDetails {

public static final String REQUIRED = "Required";
public static final String OPTIONAL = "Optional";

@JsonProperty(value = REQUIRED, required = true)
private List<String> required;

@JsonProperty(value = OPTIONAL, required = true)
private List<String> optional;
}

到目前为止我对整个对象的了解:

public class FieldDefinitions {

public static final String FIELDS = "Fields";
public static final String DESCRIPTION = "Description";

@JsonProperty(value = FIELDS, required = true)
private FieldDetails fields;

@JsonProperty(value = DESCRIPTION , required = true)
private ??? descriptions;
}

最佳答案

一般来说,您始终可以映射任何 JSON反对 Map<String, Object> 。如果JSON由于许多嵌套对象而变得复杂,Jackson将自动选择正确的类型:Map对于物体和List对于数组。

您还可以像下面这样为 Description 声明类特性。

class Description {
private String label;
private String description;
// getters, setters, toString
}

整体Description是个大JSON您可以将其映射到 Map<String, Description> 。因此,它可能如下所示:

class FieldDefinitions {

public static final String FIELDS = "Fields";
public static final String DESCRIPTION = "Description";

@JsonProperty(value = FIELDS, required = true)
private FieldDetails fields;

@JsonProperty(value = DESCRIPTION, required = true)
private Map<String, Description> descriptions;

// getters, setters, toString
}

休息是一样的。示例应用程序:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.util.List;
import java.util.Map;

public class JsonApp {

public static void main(String[] args) throws Exception {
File json = new File("./resource/test.json").getAbsoluteFile();

ObjectMapper mapper = new ObjectMapper();

FieldDefinitions fields = mapper.readValue(json, FieldDefinitions.class);
System.out.println("Required");
fields.getFields().getRequired().forEach(r ->
System.out.println(r + " = " + fields.getDescriptions().get(r)));
System.out.println("Optional");
fields.getFields().getOptional().forEach(r ->
System.out.println(r + " = " + fields.getDescriptions().get(r)));
}
}

对于给定的JSON payload打印:

Required
ftp.hostname = Description{label='SFTP Hostname', description='SFTP server hostname or IP address'}
Optional
ftp.rootDirectory = Description{label='Root Directory', description='The root path on the Data Store accessible by this connector'}

关于java - 如何为 JsonObjects 的 JsonObject 创建 Jackson XML POJO 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54793423/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com