【问题标题】:Strange Pojo generated for a Json object by jsonschema2pojo.orgjsonschema2pojo.org 为 Json 对象生成的奇怪 Pojo
【发布时间】:2025-12-07 05:35:01
【问题描述】:

这是我第一次在这里发布问题:

我有一个这样的 json 结构:

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

生成的 POJO 如下所示:

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

如果 POJO 指示只有一个名称值属性,我知道设置这些属性很简单,比如 element.setName(), element.setValue。

但是我如何在这个 POJO 中设置值以生成一个 Json 结构作为我的示例?

【问题讨论】:

  • Convert Pojo to JSON的可能重复
  • 也看看 GSON。 code.google.com/p/google-gson
  • 您说得对,POJO 似乎不适合您的 Json。您的 Json sn-p 不是正确的 json 对象-您只是缺少左括号和右括号吗?您使用什么库来生成 POJO?
  • 我使用 jsonschema2pojo.org 内部似乎使用 Jackson。是的,我只是缺少大括号。我使用的是有效的。

标签: java json jackson


【解决方案1】:

您似乎只是忘记了它生成的第二个类。对于您的 json:

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

工具http://www.jsonschema2pojo.org/ 生成以下代码(当设置为 Jackson 2.x 注释和源类型 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);
}

}

所以实际上是 Example 类是您想要的 POJO,它包含 Element POJO 的列表。 Element 是为数组中的对象类型生成的 POJO。请注意,该类名为Example,因为这是 jsonschema2pojo.org 设置的*对象的默认名称。您可以在右侧的输入字段中更改它。

【讨论】:

  • 感谢您的回答。问题是我在我的代码中使用了相同的结构,但是使用该结构生成的 xml(我稍后使用它转换为 json)看起来像这样: name1value1name2value2 而我希望它像 name1value1 name2value2
  • 请原谅格式不好。在发表评论时,看起来按下返回键最终会提交评论。
  • 嗯,这是一个完全不同的问题 :) 我仍然不清楚 - 您采用上述 POJO 并使用它们来生成 XML?如何?请更新您的问题或提出新问题。
  • 感谢您的跟进...我能够让它工作。生成的 POJO 仅具有名称、值属性。我更正了它,包括名称、值属性的映射(带有该映射的 getter/setter)和保存映射的列表属性。现在,当我创建一些映射并将其分配给列表时,我只需说类似 element.setList(list_of_maps of name-value pairs),它会生成我想要的格式的 xsd/json。