Java类中的JSON使用POJO方法

时间:2017-10-11 14:54:16

标签: java json pojo jackson2 jsonschema2pojo

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

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的类,并为属性设置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我不会工作可能因为我没有宣布一切正确,因为我不完全理解这种方法

2 个答案:

答案 0 :(得分:2)

http://www.jsonschema2pojo.org/上 网站 来源类型: 选择JSON选项 然后复制粘贴它将起作用的输出

答案 1 :(得分:2)

而不是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);
}

}