gpt4 book ai didi

java - 如何忽略变量名但序列化值 - jackson fasterxml

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:27 27 4
gpt4 key购买 nike

我从代码中得到以下输出:{“列表”:[{“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());
}
}

更新这个问题 - 让它更上一层楼给我:

{"列表":[{" map ":{"x":"y","x1":"y1"}},{" map ":{"a1":"b1","a":"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 注释,其文档说:

Marker annotation similar to XmlValue that indicates that results of the annotated "getter" method (which means signature must be that of getters; non-void return type, no args) is to be used as the single value to serialize for the instance. Usually value will be of a simple scalar type (String or Number), but it can be any serializable type (Collection, Map or 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());
}
}

关于java - 如何忽略变量名但序列化值 - jackson fasterxml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34691013/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com