简体   繁体   中英

How to ignore variable name but serialize value - jackson fasterxml

I have the following output from the code: {"list":[{"x":"y"},{"a":"b"}]}

Instead i want to get the output as [{"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"}}]}

I want [{"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:

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).

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; on Car class and did the following.

  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. 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. (or i could not get it working)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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