创建阵列时Jackson2 Java来JSON数组忽略字段名 [英] Jackson2 Java to Json array ignore field names when creating array

查看:236
本文介绍了创建阵列时Jackson2 Java来JSON数组忽略字段名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个Java REST端点,返回一个JSON数据阵列的jQuery FLOT图表插件被消耗掉。

在最低限度,必须是数字即数组用于FLOT JSON数据。

  [X1,Y1],[X2,Y2],...]

由于我已经Point对象的列表在Java中即。

 列表<点和GT;数据=新的ArrayList<>();

,其中点被定义为

 公共类点{    私人最终整数x;
    私人最终码整数Y;    公共点(整数x,整数Y){
        this.x = X;
        this.y = Y;
    }    ...
 }

我需要什么样的方法或Jackson2注解,如果有的话,做提上Java对象,以获得正确的JSON格式。目前我得到的输出格式为:

  [{X:X1,Y:Y1},{X:X2,Y:Y2} ...]

当我需要的格式为:

  [X1,Y1],[X2,Y2] ...]


解决方案

您可以编写自定义的串行

 进口java.io.IOException异常;。进口组织codehaus.jackson.JsonGenerator;
。进口组织codehaus.jackson.JsonProcessingException;
。进口组织codehaus.jackson.map.JsonSerializer;
。进口组织codehaus.jackson.map.SerializerProvider;公共类CustomPointSerializer扩展JsonSerializer<点和GT; {    @覆盖
    公共无效连载(点对点,JsonGenerator根,SerializerProvider提供商)抛出IOException异常,JsonProcessingException {
        gen.writeStartArray();
        gen.writeNumber(point.getX());
        gen.writeNumber(point.getY());
        gen.writeEndArray();
    }
}

那么你可以自定义序列化器类设置为你的

 进口组织codehaus.jackson.map.annotate.JsonSerialize。@JsonSerialize(使用= CustomPointSerializer.class)
公共类点{    私人整数x;
    私人码整数Y;    公共点(整数x,整数Y){
        this.x = X;
        this.y = Y;
    }    公共整数的getX(){
        返回X;
    }    公共无效setX的(整数x){
        this.x = X;
    }    公共整数的getY(){
        返回是;
    }    公共无效塞蒂(整数Y){
        this.y = Y;
    }
}

和尝试

  ObjectMapper映射器=新ObjectMapper();
清单<点和GT;点=新的ArrayList<点和GT;();
points.add(新点(1,2));
points.add(新点(2,3));
的System.out.println(mapper.writeValueAsString(点));

在code产生以下结果。

  [1,2],[2,3]

希望这有助于。

I'm trying to create a Java REST endpoint which returns a JSON data array to be consumed by JQuery FLOT charting plugin.

At a minimum, the JSON data for FLOT needs to be an array of numbers i.e.

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

Given I have a List of Point objects in Java i.e.

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

where Point is defined as

public class Point {

    private final Integer x;
    private final Integer y;

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

    ...
 }

What methods or Jackson2 annotations, if any, do I need to put on the Java objects to get the correct JSON format. Currently I am getting the output in this format:

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

When I need this format:

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

解决方案

You can write a custom Point serializer

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

then you can set your custom serializer class to your Point class

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

and try it

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

the code produces the following result

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

hope this helps.

这篇关于创建阵列时Jackson2 Java来JSON数组忽略字段名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆