返回数组与JSON

问题描述:

我正在返回一个向量与对象FLineType返回数组与JSON

回归了Methode看起来像

@GET 
@Path("/linetypes") 
@Produces(MediaType.APPLICATION_JSON) 
public Vector<FLineType> getLineTypes(){ 
    db = Database.getInstance(); 
    return db.getLineTypes(); 
} 

FLineType看起来像

import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
public class FLineType { 
    private int id; 
    private String bez; 

public FLineType(){} 

public FLineType(int id, String bez) { 
    super(); 
    this.id = id; 
    this.bez = bez; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public String getBez() { 
    return bez; 
} 

public void setBez(String bez) { 
    this.bez = bez; 
} 

} 

和我收到的数据在Java对象看起来像

{"fLineType":[{"bez":"Linie 1","id":"1"},{"bez":"Linie 2","id":"2"},{"bez":"Linie 3","id":"3"},{"bez":"Powerplay 1","id":"4"},{"bez":"Powerplay 2","id":"5"},{"bez":"Unterzahl 1","id":"6"},{"bez":"Unterzahl 2","id":"7"}]} 

但我只想在[]括号中的JSON数组数据。没有对象名称。 因为我收到时收到JSONException that the Data must start with [

我对REST服务器使用泽西1.18。

有人可以帮我吗?

+0

为什么'Vector'?为什么不是一个简单的'List'或甚至一个'Collection'? – fge

+0

@fge:我得到了与'List'或'Array'相同的结果 – Stone

+0

然后使用'List'。 'Vector'已过时。 – fge

我通过将这些行添加到我的web.xml文件中解决了这个问题。

<init-param> 
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> 
    <param-value>true</param-value> 
</init-param>