将此 json 映射到 java object (pojo)
Mapping this json to java object (pojo)
我必须映射一个看起来像这样的 json,它基本上是一个 object,它可以再次包含与 child 相同的 object,然后那个也可以再次包含相同的 object 。我如何才能将其映射到 java pojo's?
这是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 的实例
您可以使用 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);
}
}
您的 JSON 无效,您的对象未正确使用对象与列表。请验证您的示例 JSON。 "children" : [ "group" : [
无效。
您的错误发生是因为在您的 java 对象指定 {
时遇到 [
。
您可以删除 Children 和 ChildrenGroup,并将 GroupList 嵌套在 Group 中。
我必须映射一个看起来像这样的 json,它基本上是一个 object,它可以再次包含与 child 相同的 object,然后那个也可以再次包含相同的 object 。我如何才能将其映射到 java pojo's?
这是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 的实例您可以使用 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);
}
}
您的 JSON 无效,您的对象未正确使用对象与列表。请验证您的示例 JSON。 "children" : [ "group" : [
无效。
您的错误发生是因为在您的 java 对象指定 {
时遇到 [
。
您可以删除 Children 和 ChildrenGroup,并将 GroupList 嵌套在 Group 中。