0

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?

Jakub Kotowski
  • 6,811
  • 23
  • 36
user3325862
  • 111
  • 2
  • 6
  • possible duplicate of [Convert Pojo to JSON](http://stackoverflow.com/questions/9593409/convert-pojo-to-json) – MarCrazyness Feb 27 '14 at 23:11
  • Also take a look at GSON. https://code.google.com/p/google-gson/ – MarCrazyness Feb 27 '14 at 23:11
  • You're right that the POJO doesn't seem to fit your Json. Your Json snippet is not a correct json object - are you just missing opening and closing brackets? What library did you use to generate the POJO? – Jakub Kotowski Feb 27 '14 at 23:13
  • I use http://www.jsonschema2pojo.org/ which internally seems to use Jackson.Yes, I am just missing the braces. I use a valid one. – user3325862 Feb 27 '14 at 23:32

1 Answers1

1

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.

Jakub Kotowski
  • 6,811
  • 23
  • 36
  • Thank you for the answer. The thing is I use the same in my code, but with that structure the generated xml (I use that to convert to json later) looks like this : name1 value1 name2 value2 whereas I want it to be like name1 value1 name2 value2 – user3325862 Feb 28 '14 at 00:45
  • Please excuse the poor formatting. Looks like pressing the return key ends up submitting the comment, when posting a comment. – user3325862 Feb 28 '14 at 00:50
  • well, that's an entirely different question :) and it's still not clear to me - you take the above POJOs and use them to generate the XML? How? Please update your question or ask a new one. – Jakub Kotowski Feb 28 '14 at 01:02
  • Thank you for following up...I was able to get it to work. The POJO that was generated just had name, value properties. I corrected it, to include a map of name, value properties(with getter/setters for that map) and a list property to hold the maps. Now when I create some maps and assign it to the list, I just say something like, element.setList(list_of_maps of name-value pairs), and it generatesthe xsd/json in the format I want. – user3325862 Feb 28 '14 at 19:54