JSON,JAXB,格式问题

JSON, JAXB, Formatting issues

本文关键字:问题 格式 JAXB JSON      更新时间:2023-09-26

我在数据格式问题上遇到了麻烦。我有一个简单的JaxB类

@XmlRootElement(name="")
public class MyProgressResponse {
    private int weight;
    private long date;
    /**
     * Weight is treated as a Y Axis.
     * @return
     */
    @XmlElement(name="y")
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
    /**
     * This is a UTC format of a time.
     * value is a number of milliseconds between a specified date and midnight January 1 1970
     * This is also treated as a X-Axis
     * @return 
     */
    @XmlElement(name="x")
    public long getDate() {
        return date;
    }
    public void setDate(long date) {
        this.date = date;
    }
}

我想要填充返回数据的 REST 服务。 像这样

@GET
@Path("/my")
@Produces(MediaType.APPLICATION_JSON)
public MyProgressResponse[] getProgressResponse(){
    // Get the data from DB
    // Here the getDate will give me List<MyProgressResponse>
    return getData().toArray(new MyProgressResponse[0]);
}

现在我收到的 JSON 就像

[
 {
   {
     "x": 1335499200000,
     "y": 85
   }
 },
 {
   {
     "x": 1334894400000,
     "y": 84
   }
 },
 ....
]

但我的要求是得到没有一个额外的块{ }.

[
   {
     "x": 1335499200000,
     "y": 85
   },
   {
     "x": 1334894400000,
     "y": 84
   },
   ....
]

我想在HighChart中使用它。我可以在收到数据后用 JS 格式化数据,但它会得到额外的时间,我不想这样。

任何人都可以帮助我格式化数据吗

谢谢

塔哈·艾哈迈德·汗

将方法的返回类型更改为 ArrayList:

@GET @Path("/my") 
@Produces(MediaType.APPLICATION_JSON) 
public ArrayList<MyProgressResponse> getProgressResponse(){     
// Get the data from DB     
// Here the getDate will give me List<MyProgressResponse>
ArrayList<MyProgressResponse> response=(ArrayList<MyProgressResponse>) getData();
return response; 
} 

还要使你的 Bean 类实现可序列化。