JSON,JAXB,格式化问题

时间:2012-04-27 09:37:53

标签: java javascript rest java-ee jaxb

我遇到了数据格式问题。我有一个简单的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中格式化数据,但是会得到额外的时间,我不想要那样。

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

谢谢,

塔尔哈·艾哈迈德·汗

1 个答案:

答案 0 :(得分:0)

将方法的返回类型更改为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类实现Serializable。