Java Jackson序列化为数组

时间:2014-05-28 05:41:05

标签: java jackson

我如何对以下类进行serilize以获得像{“params”:[[],[],1,2 ....]}这样的值。任何帮助,将不胜感激。

    public class Search{
public List<Double> minLatLng=new ArrayList<Double>();
    public List<Double> maxLatLng=new ArrayList<Double>(); //These are bounding co-ordinates
        public List<Integer> ids=new ArrayList<Integer>();
        public List<Integer> owner=new ArrayList<Integer>();
        public float minScore;
        public float maxScore;
        public long minLength;
        public long maxLength;
        public String flag;
        public String language;
        public String keywords;
        public List<Integer> groupIds=new ArrayList<Integer>();
        public List<Integer> characteristic=new ArrayList<Integer>();
        public List<Integer> theme=new ArrayList<Integer>();
        public boolean acitivity;
        public List<Integer> activityOwner=new ArrayList<Integer>();
        public boolean event;
        public List<Integer> eventOwner=new ArrayList<Integer>();
        public List<Integer> favouriteUser=new ArrayList<Integer>();
    }

    The out put should be look like { "params":[[],[],1,2....]} how can i use jackson for getting this value

1 个答案:

答案 0 :(得分:1)

您可以将一个带有@JsonValue注释的方法添加到您的类中,该方法将从反射中获取字段值并以数组映射的形式返回它们。

以下是一个例子:

public class JacksonBeanToArray {
    public static class Search {
        public List<Double> minLatLng=new ArrayList<Double>();
        public List<Double> maxLatLng=new ArrayList<Double>(); //These are bounding co-ordinates
        public List<Integer> ids=new ArrayList<Integer>();
        public List<Integer> owner=new ArrayList<Integer>();
        public float minScore;
        public float maxScore;
        public long minLength;
        public long maxLength;
        public String flag;
        public String language;
        public String keywords;
        public List<Integer> groupIds=new ArrayList<Integer>();
        public List<Integer> characteristic=new ArrayList<Integer>();
        public List<Integer> theme=new ArrayList<Integer>();
        public boolean acitivity;
        public List<Integer> activityOwner=new ArrayList<Integer>();
        public boolean event;
        public List<Integer> eventOwner=new ArrayList<Integer>();
        public List<Integer> favouriteUser=new ArrayList<Integer>();

        @JsonValue
        public Map<String, Object[]> getAsParamMap() throws IllegalAccessException {
            Object[] result = new Object[getClass().getFields().length];
            for (int i = 0; i < result.length; i++) {
                result[i] = getClass().getFields()[i].get(this);
            }
            return Collections.singletonMap("params", result);
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        Search search = new Search();
        search.acitivity = true;
        search.maxLatLng = Arrays.asList(0.44, 5.);
        search.maxScore = 4;
        search.language = "EN";
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(search));
    }

}

输出:

{
  "params" : [ [ ], [ 0.44, 5.0 ], [ ], [ ], 0.0, 4.0, 0, 0, null, "EN", null, [ ], [ ], [ ], true, [ ], false, [ ], [ ] ]
}