如何忽略变量名称而序列化值-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());
  }
 }

此问题的更新-使其更上一层楼给我:

{"list":〔{"map":{"x":"y","x1":"y1"}},{"映射":{"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注释,其文档显示:

标记注释类似于XmlValue,指示带注释的"getter"方法的结果(这意味着签名必须是getter的签名;非void返回类型,没有args)将用作实例的单个值进行序列化。通常,值是一个简单的标量类型(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课堂上,并做了以下操作。

  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不适用于非公共字段。(或者我无法让它工作)

最新更新