将此json映射到java对象(pojo)

时间:2017-06-07 14:29:05

标签: java json jackson pojo

我必须映射一个看起来像这样的json,它基本上是一个对象,它可以再次包含与子对象相同的对象,然后该对象也可以再次包含相同的对象。我怎样才能将它映射到java pojo?

这是json:

    {
"group": [
            {
                "name": "Beheerders",
                "desc": "Beheerders",
                "children" : [
                    "group" : [
                        {
                            "name": "Beheerders",
                            "desc": "Beheerders"
                        },
                        {
                            "name": "Beheerders",
                            "desc": "Beheerders"
                        },
                        {
                            "name": "Beheerders",
                            "desc": "Beheerders"
                            "children": [
                                "group" : [
                                {
                                    "name": "Beheerders",
                                    "desc": "Beheerders"
                                },
                                {
                                    "name": "Beheerders",
                                    "desc": "Beheerders"
                                }
                        }

            }
        ]
    }

我有这4个pojo:

Group.java

        private String name;
        private String desc;
        private Children children;

//getters & Setters & toString

GroupList.java

 private ArrayList<Group> group;

    public void setGroup(ArrayList<Group> group) {
        this.group = group;
    }

    public ArrayList<Group> getGroup() {
        return this.group;
    }

Children.java

   private ArrayList<ChildrenGroup> group;

    public ArrayList<ChildrenGroup> getGroup() {
        return this.group;
    }

    public void setGroup(ArrayList<ChildrenGroup> group) {
        this.group = group;
    }

childrengroup.java

    private String name;
    private String desc;
    private Children Children;

//Getters & Setters & toString

这对我不起作用,我总是收到这个错误:

com.fasterxml.jackson.databind.JsonMappingException:无法从START_OBJECT标记中反序列化java.util.ArrayList的实例

2 个答案:

答案 0 :(得分:0)

您可以使用jsonschema2pojo等在线工具从JSON生成POJO。

如果我正确理解你正在尝试转换smth,如下所示:

{
  "g":[
    {
      "a":"1",
      "c":"2"
    },
    {
      "a":"2",
      "c":"3",
      "d":[
        {
          "g":[
            {"a":"3",
            "c":"4"},
            {"a":"5",
            "c":"6"}
            ]
        }
        ]
    }
  ]
} 

这个的输出将是:

-----------------------------------com.example.D.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class D {

private List<G_> g = null;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public List<G_> getG() {
return g;
}

public void setG(List<G_> g) {
this.g = g;
}

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

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

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

package com.example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Example {

private List<G> g = null;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public List<G> getG() {
return g;
}

public void setG(List<G> g) {
this.g = g;
}

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

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

}
-----------------------------------com.example.G.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class G {

private String a;
private String c;
private List<D> d = null;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public String getA() {
return a;
}

public void setA(String a) {
this.a = a;
}

public String getC() {
return c;
}

public void setC(String c) {
this.c = c;
}

public List<D> getD() {
return d;
}

public void setD(List<D> d) {
this.d = d;
}

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

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

}
-----------------------------------com.example.G_.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;

public class G_ {

private String a;
private String c;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public String getA() {
return a;
}

public void setA(String a) {
this.a = a;
}

public String getC() {
return c;
}

public void setC(String c) {
this.c = c;
}

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

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

}

答案 1 :(得分:0)

您的JSON无效且您的对象无法正确使用对象与列表。请验证您的示例JSON。 "children" : [ "group" : [无效。

您的错误正在发生,因为当您的java对象指定[时遇到{

您可以删除Children和ChildrenGroup,也可以在Group内嵌套GroupList。