我正在尝试为以下JSON结构创建POJO。 Fields
节点很容易连线,但是我不确定如何使用注释来连线Description
节点。如果我已经为该节点定义了JSON结构,那么我将创建一个JsonObjects的JsonArray,这将使Java类变得容易,但是由于我没有这么做,所以我需要弄清楚如何序列化以下结构: / p>
{
"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;
}
答案 0 :(得分:1)
那是结构。
phrase = ("""<d:entry xmlns:d="http://www.apple.com/DTDs/DictionaryService-1.0.rng" id="test" d:title="test">
"""<d:index d:value="test" d:title="test"/><d:index d:value="test2" d:title="test2"/>"""
"""<div class="ODECN"><div class="extras"><div class="phrase"><span class="word_title"><i>test</i></span>:
"""</d:entry>""")
答案 1 :(得分:1)
通常,您始终可以将任何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'}