为Json Array创建映射类

时间:2019-04-02 11:50:31

标签: java arrays json jackson

在Json文件下方给出

[
  "a",
  "b",
  "c"
]

我需要为上述Json创建POJO类。我尝试了以下代码

public class Elements{
  public String element;
  public Elements(String element){
    this.element = element;
  }
}

.........

public class OuterElement{
   Elements[] elements;
   //Getter and Setter
}

但是我得到了例外

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of [...] out of START_ARRAY token

在这种情况下,POJO类应该如何?

4 个答案:

答案 0 :(得分:1)

您可以使用数组或列表:

["a","b","c"]->字符串[]元素;

["a","b","c"]->列出元素;

{"elements":["a","b","c"]}->类YourPOJO {String[] elements;}

请记住,您需要getter,setter和默认构造函数

答案 1 :(得分:0)

您需要创建一个带有List<String>参数的构造函数,并用@JsonCreator对其进行注释。下面是一个简单的示例:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Arrays;
import java.util.List;

public class Test {

    public static void main(String[] args) throws Exception {
        String json = "[\"a\",\"b\",\"c\"]";

        ObjectMapper mapper = new ObjectMapper();
        OuterElement outerElement = mapper.readValue(json, OuterElement.class);

        System.out.println(outerElement);
    }
}

class Element {

    private String value;

    public Element(String value) {
        this.value = value;
    }

    // getters, setters, toString
}

class OuterElement {

    private Element[] elements;

    @JsonCreator
    public OuterElement(List<String> elements) {
        this.elements = new Element[elements.size()];
        int index = 0;
        for (String element : elements) {
            this.elements[index++] = new Element(element);
        }
    }

    // getters, setters, toString
}

上面的代码显示:

OuterElement{elements=[Element{value='a'}, Element{value='b'}, Element{value='c'}]}

答案 2 :(得分:0)

通过您的pojo,我们将获得如下数据,

Java代码

OuterElement outerElement=new OuterElement();
        outerElement.setElements(new Elements[]{new Elements("a"),new Elements("b"),new Elements("c")});

还有数据

{
    "elements": [
        {
            "element": "a"
        },
        {
            "element": "b"
        },
        {
            "element": "c"
        }
    ]
}

这就是为什么json映射器转换失败,数据映射器期望的是对象,但是您提交的是产生"Can not deserialize instance of [...] out of START_ARRAY token"

的数组

您可以像下面这样使用pojo,

public class Elements {

    @JsonProperty("0")
    public String element;

    public String getElement() {
        return element;
    }

    public void setElement(String element) {
        this.element = element;
    }

    public Elements(String element) {
        super();
        this.element = element;
    }



}

答案 3 :(得分:0)

您可以使用http://www.jsonschema2pojo.org/将JSON解析为POJO

请注意,如果您的完整JSON是

["a","b","c"]只能将其解析为列表数组。与其将其映射到对象,不如尝试以其他方式访问JSON。有关示例,请参见@jschnasse的answer

但是,如果您通常使用JSON对象作为

{
  "alphabet": ["a","b","c"]
}

然后http://www.jsonschema2pojo.org/将为您生成下一个POJO:

package com.example;

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({
        "alphabet"
})
public class Example {

    @JsonProperty("alphabet")
    private List<String> alphabet = null;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonProperty("alphabet")
    public List<String> getAlphabet() {
        return alphabet;
    }

    @JsonProperty("alphabet")
    public void setAlphabet(List<String> alphabet) {
        this.alphabet = alphabet;
    }

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

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

}