user3325862
user3325862

Reputation: 121

Strange Pojo generated for a Json object by jsonschema2pojo.org

This is the first time I am posting a question here :

I have a json structure of this kind :

    "{  \"element\": ["
        + "    {"
        + "      \"name\": \"name1\",\n"
        + "      \"value\": \"Married\"\n"
        + "    },"
        + "    {"
        + "      \"name\": \"name2\",\n"
        + "      \"value\": 0\n"
        + "    },"
        + "    {"
        + "      \"name\": \"name3\",\n"
        + "      \"value\": 0\n"
        + "    }"
        + "  ] }"

The POJO that is generated looks like this :

public class Element {
    private String name;
    private String value;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

If there were just one name value property as the POJO indicates, I understand that it is straightforward to just set those properties, saying something like element.setName(), element.setValue.

But how do I set values in this POJO for it to result in a Json structure as my example?

Upvotes: 0

Views: 3987

Answers (1)

Jakub Kotowski
Jakub Kotowski

Reputation: 7571

It seems you just forgot about the second class it generates. For your json:

{
    "element": [ 
      { "name": "name1",
        "value": "Married"
      }, 
      { "name": "name2",
        "value": 0
      },
      { "name": "name3",
        "value": 0
      }  ]
}

The tool, http://www.jsonschema2pojo.org/, generates the following code (when set to Jackson 2.x annotations and source type JSON):

-----------------------------------com.example.Elemant.java-----------------------------------

package com.example;

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

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("com.googlecode.jsonschema2pojo")
@JsonPropertyOrder({
"name",
"value"
})
public class Elemant {

@JsonProperty("name")
private String name;
@JsonProperty("value")
private Integer value;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("name")
public String getName() {
return name;
}

@JsonProperty("name")
public void setName(String name) {
this.name = name;
}

@JsonProperty("value")
public Integer getValue() {
return value;
}

@JsonProperty("value")
public void setValue(Integer value) {
this.value = value;
}

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

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

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

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

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("com.googlecode.jsonschema2pojo")
@JsonPropertyOrder({
"element"
})
public class Example {

@JsonProperty("element")
private List<Elemant> element = new ArrayList<Elemant>();
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("element")
public List<Elemant> getElement() {
return element;
}

@JsonProperty("element")
public void setElement(List<Elemant> element) {
this.element = element;
}

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

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

}

So it is in fact the class Example that is the POJO you want, it contains a list of the Element POJOs. Element is the POJO generated for type of object in the array. Note that the class is called Example because that's the default name for the top-level object set by jsonschema2pojo.org. You can change it in the input field on the right side.

Upvotes: 2

Related Questions