【问题标题】:return array with objects in java with json使用json在java中返回带有对象的数组
【发布时间】:2014-05-12 00:49:25
【问题描述】:

我正在返回一个带有 Object FLineType 的向量

返回方法看起来像

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

}

我收到的数据看起来像

{"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 [

我将 jersey 1.18 用于 REST 服务器。

谁能帮帮我?

【问题讨论】:

  • 为什么是Vector?为什么不是简单的List 甚至是Collection
  • @fge: 我得到了与ListArray 相同的结果
  • 然后使用ListVector 已过时。
  • 我得到了同样的错误结果。
  • 我知道这不能解决你的问题,但这是 2014 年,你不应该使用 Vector -- 无论如何,我怀疑问题出在 @XmlRootElement;你真的需要吗?

标签: java json rest jersey getjson


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多