JSON 在 Java class 中使用 POJO 方法

JSON in Java class using POJO approach

所以我想在我的编码中使用 POJO 方法来处理 JSON 文件,所以我将有一个带有 getter 和 setter 的 class 并在其中声明属性。我理解了这个原理,但我仍然缺乏理解如何处理例如我在网上找到的以下测试 JSON 文件到 java class.[=16= 中的想法]

JSON:

{
"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML", "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
}
}

所以我理解原则的方式我现在必须创建一个名为 Glossary 的 class 并为属性设置 setter 和 getter,所以像这样:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"title",
"GlossDiv",
 // GlossList, GlossEntry, ID, SortAs and so on also here probably?
})
public class Glossary {

@JsonProperty("title")
private String title;
@JsonProperty("GlossDiv")
private Object glossDiv; // I am already at this point here not sure is it an Object? 
//I mean GlossDiv has other properties in itself so what do I put here? 
//since it is not an Array I don't use a Map or List do I ?


@JsonProperty("title")
public String getTitle() {
return title;
}

@JsonProperty("title")
public void setTitle(String title) {
this.title = title;
}
}

我还在网上找到了这个网站:http://www.jsonschema2pojo.org/ 但只是复制并粘贴 JSON 我已经无法使用可能是因为我没有完全理解我没有声明一切正确方法

http://www.jsonschema2pojo.org/ 网站在 来源类型: 选择 JSON 选项 然后复制粘贴输出就可以了

我们应该使用 class 名称而不是 glossDiv 的对象,因为这里的 GlossDiv 是给定 JSON.

中的另一个对象
@JsonProperty("GlossDiv")
private GlossDiv glossDiv;

上面 JSON 到 java 对象的转换如下所示

com.json.pojo.ExploreJson.java

package com.json.pojo;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"glossary"
})
public class ExploreJson {

@JsonProperty("glossary")
private Glossary glossary;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("glossary")
public Glossary getGlossary() {
return glossary;
}

@JsonProperty("glossary")
public void setGlossary(Glossary glossary) {
this.glossary = glossary;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.json.pojo.GlossDef.java-----------------------------------

package com.json.pojo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"para",
"GlossSeeAlso"
})
public class GlossDef {

@JsonProperty("para")
private String para;
@JsonProperty("GlossSeeAlso")
private List<String> glossSeeAlso = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("para")
public String getPara() {
return para;
}

@JsonProperty("para")
public void setPara(String para) {
this.para = para;
}

@JsonProperty("GlossSeeAlso")
public List<String> getGlossSeeAlso() {
return glossSeeAlso;
}

@JsonProperty("GlossSeeAlso")
public void setGlossSeeAlso(List<String> glossSeeAlso) {
this.glossSeeAlso = glossSeeAlso;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.json.pojo.GlossDiv.java-----------------------------------

package com.json.pojo;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"title",
"GlossList"
})
public class GlossDiv {

@JsonProperty("title")
private String title;
@JsonProperty("GlossList")
private GlossList glossList;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("title")
public String getTitle() {
return title;
}

@JsonProperty("title")
public void setTitle(String title) {
this.title = title;
}

@JsonProperty("GlossList")
public GlossList getGlossList() {
return glossList;
}

@JsonProperty("GlossList")
public void setGlossList(GlossList glossList) {
this.glossList = glossList;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.json.pojo.GlossEntry.java-----------------------------------

package com.json.pojo;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"ID",
"SortAs",
"GlossTerm",
"Acronym",
"Abbrev",
"GlossDef",
"GlossSee"
})
public class GlossEntry {

@JsonProperty("ID")
private String iD;
@JsonProperty("SortAs")
private String sortAs;
@JsonProperty("GlossTerm")
private String glossTerm;
@JsonProperty("Acronym")
private String acronym;
@JsonProperty("Abbrev")
private String abbrev;
@JsonProperty("GlossDef")
private GlossDef glossDef;
@JsonProperty("GlossSee")
private String glossSee;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("ID")
public String getID() {
return iD;
}

@JsonProperty("ID")
public void setID(String iD) {
this.iD = iD;
}

@JsonProperty("SortAs")
public String getSortAs() {
return sortAs;
}

@JsonProperty("SortAs")
public void setSortAs(String sortAs) {
this.sortAs = sortAs;
}

@JsonProperty("GlossTerm")
public String getGlossTerm() {
return glossTerm;
}

@JsonProperty("GlossTerm")
public void setGlossTerm(String glossTerm) {
this.glossTerm = glossTerm;
}

@JsonProperty("Acronym")
public String getAcronym() {
return acronym;
}

@JsonProperty("Acronym")
public void setAcronym(String acronym) {
this.acronym = acronym;
}

@JsonProperty("Abbrev")
public String getAbbrev() {
return abbrev;
}

@JsonProperty("Abbrev")
public void setAbbrev(String abbrev) {
this.abbrev = abbrev;
}

@JsonProperty("GlossDef")
public GlossDef getGlossDef() {
return glossDef;
}

@JsonProperty("GlossDef")
public void setGlossDef(GlossDef glossDef) {
this.glossDef = glossDef;
}

@JsonProperty("GlossSee")
public String getGlossSee() {
return glossSee;
}

@JsonProperty("GlossSee")
public void setGlossSee(String glossSee) {
this.glossSee = glossSee;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.json.pojo.GlossList.java-----------------------------------

package com.json.pojo;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"GlossEntry"
})
public class GlossList {

@JsonProperty("GlossEntry")
private GlossEntry glossEntry;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("GlossEntry")
public GlossEntry getGlossEntry() {
return glossEntry;
}

@JsonProperty("GlossEntry")
public void setGlossEntry(GlossEntry glossEntry) {
this.glossEntry = glossEntry;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.json.pojo.Glossary.java-----------------------------------

package com.json.pojo;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"title",
"GlossDiv"
})
public class Glossary {

@JsonProperty("title")
private String title;
@JsonProperty("GlossDiv")
private GlossDiv glossDiv;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("title")
public String getTitle() {
return title;
}

@JsonProperty("title")
public void setTitle(String title) {
this.title = title;
}

@JsonProperty("GlossDiv")
public GlossDiv getGlossDiv() {
return glossDiv;
}

@JsonProperty("GlossDiv")
public void setGlossDiv(GlossDiv glossDiv) {
this.glossDiv = glossDiv;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}