简体   繁体   English

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

[英]How to ignore variable name but serialize value - jackson fasterxml

I have the following output from the code: {"list":[{"x":"y"},{"a":"b"}]} 我从代码中得到以下输出:{“list”:[{“x”:“y”},{“a”:“b”}]}

Instead i want to get the output as [{"x":"y"},{"a":"b"}] 相反,我希望输出为[{“x”:“y”},{“a”:“b”}]

Code is below. 代码如下。

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());
  }
 }

Update to this problem - making it one more level Gives me: 更新到这个问题 - 再提高一个级别给我:

{"list":[{"map":{"x":"y","x1":"y1"}},{"map":{"a1":"b1","a":"b"}}]} { “清单”:[{ “地图”:{ “×”: “Y”, “×1”: “Y1”}},{ “地图”:{ “A1”: “B1”, “一个”:“B “}}]}

I want [{"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());
}
 }

You probably want to use the @JsonValue annotation, whose documentation says: 您可能想要使用@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. 类似于XmlValue的标记注释,表示带注释的“getter”方法的结果(表示签名必须是getter的结果;非void返回类型,无args)将被用作序列化实例的单个值。 Usually value will be of a simple scalar type (String or Number), but it can be any serializable type (Collection, Map or Bean). 通常,value将是一个简单的标量类型(String或Number),但它可以是任何可序列化的类型(Collection,Map或Bean)。

Here's a working example: 这是一个有效的例子:

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());
    }
}

I implemented import com.fasterxml.jackson.databind.JsonSerializable; 我实现了import com.fasterxml.jackson.databind.JsonSerializable; on Car class and did the following. 在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);
    }
  }

This removed the map keyword. 这删除了map关键字。 I could not use JsonValue as above as in my code base I'm not allowed to have a getter on Map and JsonValue did not work for non public fields. 我不能像上面的代码库一样使用JsonValue我不允许在Map上使用getter而JsonValue不适用于非公共字段。 (or i could not get it working) (或者我无法让它工作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM