【问题标题】:Jackson2 Java to Json array ignore field names when creating arrayJackson2 Java到Json数组在创建数组时忽略字段名称
【发布时间】:2014-06-14 04:15:13
【问题描述】:

我正在尝试创建一个 Java REST 端点,该端点返回一个 JSON 数据数组,供 JQuery FLOT 图表插件使用。

至少,FLOT 的 JSON 数据需要是一个数字数组,即

[ [x1, y1], [x2, y2], ... ]

鉴于我在 Java 中有一个 Point 对象列表,即

List<Point> data = new ArrayList<>();

其中Point定义为

public class Point {

    private final Integer x;
    private final Integer y;

    public Point(Integer x, Integer y) {
        this.x = x;
        this.y = y;
    }

    ...
 }

我需要在 Java 对象上放置什么方法或 Jackson2 注释(如果有)以获得正确的 JSON 格式。目前我得到这种格式的输出:

[{x:x1, y:y1}, {x:x2, y:y2} ...]

当我需要这种格式时:

[[x1,y1], [x2,y2] ...]

【问题讨论】:

  • 你需要的不是一个有效的 json..
  • 我只是概述了格式之间的差异,而不是完整的语法:)
  • @mserioli 最终的 Flot 格式是有效的 JSON...数组数组...诀窍是 Ayub 需要自定义 Jackson“对象映射器/解析器”将 x,y 作为数组返回. 作为地图。 (我不确定这个映射器/解析器的 Jackson 术语是什么)
  • 好的我明白了:)

标签: java arrays json jackson jsonserializer


【解决方案1】:

您可以在返回整数数组的特殊 getter 方法上使用 @JsonView 注释。这是一个例子:

public class JacksonObjectAsArray {
    static class Point {

        private final Integer x;
        private final Integer y;

        public Point(Integer x, Integer y) {
            this.x = x;
            this.y = y;
        }

        @JsonValue
        public int[] getXY() {
            return new int[] {x, y};
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new Point(12, 45)));
    }

}

输出:

[ 12, 45 ]

【讨论】:

  • JsonValue,如示例中所写,不是答案顶部的JsonView
【解决方案2】:

你可以写一个自定义的Point序列化器

import java.io.IOException;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;

public class CustomPointSerializer extends JsonSerializer<Point> {

    @Override
    public void serialize(Point point, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonProcessingException {
        gen.writeStartArray();
        gen.writeNumber(point.getX());
        gen.writeNumber(point.getY());
        gen.writeEndArray();
    }
}

然后您可以将自定义序列化程序类设置为您的 Point

import org.codehaus.jackson.map.annotate.JsonSerialize;

@JsonSerialize(using = CustomPointSerializer.class)
public class Point {

    private Integer x;
    private Integer y;

    public Point(Integer x, Integer y) {
        this.x = x;
        this.y = y;
    }

    public Integer getX() {
        return x;
    }

    public void setX(Integer x) {
        this.x = x;
    }

    public Integer getY() {
        return y;
    }

    public void setY(Integer y) {
        this.y = y;
    }
}

试试看

ObjectMapper mapper = new ObjectMapper();
List<Point> points = new ArrayList<Point>();
points.add(new Point(1,2));
points.add(new Point(2,3));
System.out.println(mapper.writeValueAsString(points));

代码产生以下结果

[[1,2],[2,3]]

希望这会有所帮助。

【讨论】:

  • 我使用了这个答案的稍微修改过的版本,但是它有帮助
  • 我很高兴能帮上忙,问候。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-10
  • 1970-01-01
  • 2019-01-04
  • 2021-10-25
  • 2014-09-18
  • 1970-01-01
  • 2017-10-05
相关资源
最近更新 更多