[英]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.