[英]Java - Convert to correct json format using Jackson
我正在使用Jackson向Json编写Java对象。
这就是我要得到的
{
"obj": [{
"Id": 1,
"type": "type1",
"properties": [{
"name": "PropN",
"value": "ValN"
}]
}, {
"id": 2,
"type": "type",
"properties": [{
"name": "Prop3",
"value": "Val3"
}]
}]
}
这就是我需要得到的:
{
"obj": [{
"id": 1,
"type": "type",
"properties": {
"name": "Eb1"
}
},
{
"id": 2,
"type": "type",
"properties": {
"name": "Eb2"
}
}
]
}
我不知道如何从json获取属性名称和值“标签”,并且不知道如何将属性数组删除到列表中。
我的Properties数组是一个简单的POJO,而Obj类具有ArrayList属性。
有人可以告诉我如何完成这项工作吗?
谢谢。
您可以使用quicktype生成所需的确切类型。 这是您需要的JSON产生的结果:
// Converter.java
// https://stackoverflow.com/questions/49055781/java-convert-to-correct-json-format-using-jackson
// import io.quicktype.Converter;
//
// Then you can deserialize a JSON string with
//
// StackOverflow data = Converter.fromJsonString(jsonString);
package io.quicktype;
import java.util.Map;
import java.io.IOException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.core.JsonProcessingException;
public class Converter {
// Serialize/deserialize helpers
public static StackOverflow fromJsonString(String json) throws IOException {
return getObjectReader().readValue(json);
}
public static String toJsonString(StackOverflow obj) throws JsonProcessingException {
return getObjectWriter().writeValueAsString(obj);
}
private static ObjectReader reader;
private static ObjectWriter writer;
private static void instantiateMapper() {
ObjectMapper mapper = new ObjectMapper();
reader = mapper.reader(StackOverflow.class);
writer = mapper.writerFor(StackOverflow.class);
}
private static ObjectReader getObjectReader() {
if (reader == null) instantiateMapper();
return reader;
}
private static ObjectWriter getObjectWriter() {
if (writer == null) instantiateMapper();
return writer;
}
}
// StackOverflow.java
package io.quicktype;
import java.util.Map;
import com.fasterxml.jackson.annotation.*;
public class StackOverflow {
private Obj[] obj;
@JsonProperty("obj")
public Obj[] getObj() { return obj; }
@JsonProperty("obj")
public void setObj(Obj[] value) { this.obj = value; }
}
// Obj.java
package io.quicktype;
import java.util.Map;
import com.fasterxml.jackson.annotation.*;
public class Obj {
private long id;
private String type;
private Properties properties;
@JsonProperty("id")
public long getID() { return id; }
@JsonProperty("id")
public void setID(long value) { this.id = value; }
@JsonProperty("type")
public String getType() { return type; }
@JsonProperty("type")
public void setType(String value) { this.type = value; }
@JsonProperty("properties")
public Properties getProperties() { return properties; }
@JsonProperty("properties")
public void setProperties(Properties value) { this.properties = value; }
}
// Properties.java
package io.quicktype;
import java.util.Map;
import com.fasterxml.jackson.annotation.*;
public class Properties {
private String name;
@JsonProperty("name")
public String getName() { return name; }
@JsonProperty("name")
public void setName(String value) { this.name = value; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.