Search code examples
javajsonpojojackson2jsonschema2pojo

JSON in Java class using POJO approach


So I want to use the POJO approach to handle a JSON file in my coding, so I will have a class with getters and setters and declare the properties in it. I understood the principle but I am still lacking the idea to understand how I can handle for example the following test JSON file I found on the web into a java class.

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"
            }
        }
    }
}
}

so the way I understood the principle I have now to create a class called Glossary and have setters and getters for the properties so something like this:

@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;
}
}

I also found this site on the web: http://www.jsonschema2pojo.org/ but just copy & paste the JSON I have will not work probably because I am not declaring everything right since I don't fully understood the approach


Solution

  • On http://www.jsonschema2pojo.org/ website in Source type: Choose JSON option Then copy paste the output it will work