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

4
我有以下代码的输出结果: {"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());
  }
 }

这个问题的更新 - 进一步处理 会给我:

{"list":[{"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());
}
 }

2
为什么不直接调用 objectMapper.writeValue(writer, t.list) 呢? - Jon Skeet
假设我有一个List<Car>,而Car又有一个Map,那么怎么办呢?如何忽略Map,只打印出名称值,例如[{"x","y"},{...}]? - Paul Arer
最简单的解决方法是从List<Car>创建一个List<Map<...>>。不过也可能有一种方式可以配置Jackson来自动完成这个过程——你应该将此示例添加到你的问题中,以便说明你想要做什么。否则,“只需序列化t.list”的方法是最明显的方法。 - Jon Skeet
我不想重新做响应,特别是因为它们可能很大且已经固定。我希望序列化能够处理这个问题,这样只需要一次。同时,根据您的建议修改了问题。 - Paul Arer
2个回答

2
您可能想使用@JsonValue注释,其文档如下所示:

这是一个标记注释,类似于XmlValue,它表示带有“getter”方法的结果(也就是签名必须是getter;非void返回类型,无参数)将用作实例序列化的单个值。通常值将是简单标量类型(字符串或数字),但它可以是任何可序列化类型(集合、映射或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());
    }
}

1
我在Car类上实现了import com.fasterxml.jackson.databind.JsonSerializable,并进行了以下操作。
  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(或者我无法让它起作用)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接