![](/img/trans.png)
[英]How can I Serialize/De-serialize a Boolean Value from FasterXML\Jackson as an Int?
[英]How to ignore variable name but serialize value - jackson fasterxml
我從代碼中得到以下輸出:{“list”:[{“x”:“y”},{“a”:“b”}]}
相反,我希望輸出為[{“x”:“y”},{“a”:“b”}]
代碼如下。
public class Test {
List<Map> list = new ArrayList();
public static void main(String [] args){
Test t = new Test();
Map m1 = new HashMap();
m1.put("x","y");
t.list.add(m1);
Map m2 = new HashMap();
m2.put("a","b");
t.list.add(m2);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(Include.NON_EMPTY);
objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.NON_PRIVATE);
Writer writer = new StringWriter();
try {
objectMapper.writeValue(writer, t);
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("The json is:\n"+writer.toString());
}
}
更新到這個問題 - 再提高一個級別給我:
{ “清單”:[{ “地圖”:{ “×”: “Y”, “×1”: “Y1”}},{ “地圖”:{ “A1”: “B1”, “一個”:“B “}}]}
我想要{{“x”:“y”,“x1”:“y1”},{“a1”:“b1”,“a”:“b”}]
public class Test {
public class Car{
Map map = new HashMap();
}
List<Car> list = new ArrayList();
public static void main(String [] args){
Test t = new Test();
Test.Car car = t.new Car();
Map m1 = new HashMap();
m1.put("x","y");
m1.put("x1","y1");
car.map = m1;
t.list.add(car);
car = t.new Car();
Map m2 = new HashMap();
m2.put("a","b");
m2.put("a1","b1");
car.map = m2;
t.list.add(car);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(Include.NON_EMPTY);
objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.NON_PRIVATE);
Writer writer = new StringWriter();
try {
objectMapper.writeValue(writer, t);
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("The json is:\n"+writer.toString());
}
}
您可能想要使用@JsonValue
批注,其文檔說明:
類似於XmlValue的標記注釋,表示帶注釋的“getter”方法的結果(表示簽名必須是getter的結果;非void返回類型,無args)將被用作序列化實例的單個值。 通常,value將是一個簡單的標量類型(String或Number),但它可以是任何可序列化的類型(Collection,Map或Bean)。
這是一個有效的例子:
public class Test {
public static class Car {
Map map = new HashMap();
@JsonValue
public Map getMap() {
return map;
}
}
List<Car> list = new ArrayList();
@JsonValue
public List<Car> getList() {
return list;
}
public static void main(String[] args) throws IOException {
Test t = new Test();
Car car = new Car();
Map m1 = new HashMap();
m1.put("x", "y");
m1.put("x1", "y1");
car.map = m1;
t.list.add(car);
car = new Car();
Map m2 = new HashMap();
m2.put("a", "b");
m2.put("a1", "b1");
car.map = m2;
t.list.add(car);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
Writer writer = new StringWriter();
objectMapper.writeValue(writer, t);
System.out.println("The json is:\n" + writer.toString());
}
}
我實現了import com.fasterxml.jackson.databind.JsonSerializable; 在Car class上做了以下幾點。
public class Car implements JsonSerializable{
Map map = new HashMap();
@Override
public void serialize(JsonGenerator arg0, SerializerProvider arg1)
throws IOException, JsonProcessingException {
arg0.writeObject(map);
}
}
這刪除了map關鍵字。 我不能像上面的代碼庫一樣使用JsonValue我不允許在Map上使用getter而JsonValue不適用於非公共字段。 (或者我無法讓它工作)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.