1

I'm using Jackson 2.4 and I need to generate data to be process by d3.js.

d3.js want my json values to be format like this :

values : [[0, 13.5],[1, 2.5],[2, 5],[3, 41.2]]

In my Java model I have :

public class Series {

    private String key;
    private List<Entry> values;

    ...

    public void addEntry(int x, double y) {
        values.add(new Entry(x, y));
    }

    public class Entry {
        private int x;
        private double y;

        ...        
    }
}

It is only for serialization, not for deserialization, so is there a way with Jackson annotation to have the json generate as I want because for now it generates this :

values : [{x: 0, y: 13.5},{x: 1, y: 2.5},{x: 2, y: 2, 5},{x: 3, y: 41.2}]

Thanks,

4

1 回答 1

3

最简单的方法是使用@JsonValue. 尝试将此添加到您的Entry课程中:

@JsonValue
public Object[] jsonArray() {
    return new Object[]{Integer.valueOf(x), Double.valueOf(y)};
}

(您也可以返回 a double[],因为这只是将转换为 JavaScript 数字,或者使用自动装箱,但这在 IMO 中更清晰一些。)

于 2014-06-12T15:51:04.533 回答