![](/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.