gpt4 book ai didi

java - 删除 Jersey 中 Json 输出中的基础对象

转载 作者:行者123 更新时间:2023-12-01 15:02:47 25 4
gpt4 key购买 nike

我们需要在这种返回中删除基本列表对象(equipmentMetrics):

{
"equipmentMetrics": [{
"id": "BOSS|C5E02126",
"reportedHours": "499.9998",
"reportingDate": "2012-10-10"
}, {
"id": "BOSS|C5E02126",
"reportedHours": "499.9998",
"reportingDate": "2012-11-10"
}]

}

我们正在使用 Jersey 和这个对象:

     @XmlRootElement(name = "equipmentMetrics")
public class EquipmentMetricsResponse {
private String id;
...}

这个端点:

@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Path("")
public List<EquipmentMetricsResponse> postV1Default(EquipmentMetricsRequest theRequest) {...}

我相信我们希望响应看起来像这样。我们只想取出:

"equipmentMetrics":

所以我相信我们想要这个:

[{
"id": "BOSS|C5E02126",
"reportedHours": "499.9998",
"reportingDate": "2012-10-10"
}, {
"id": "BOSS|C5E02126",
"reportedHours": "499.9998",
"reportingDate": "2012-11-10"
}]

有什么帮助吗?

最佳答案

您可以使用com.google.gson.Gson库来做到这一点。

@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Path("")
public String postV1Default(EquipmentMetricsRequest theRequest) {
List<EquipmentMetricsResponse> retVal ...
...
return new Gson().toJson(retVal);
}

返回的 JSON 为

   [{
"id": "BOSS|C5E02126",
"reportedHours": "499.9998",
"reportingDate": "2012-10-10"
}, {
"id": "BOSS|C5E02126",
"reportedHours": "499.9998",
"reportingDate": "2012-11-10"
}]

关于java - 删除 Jersey 中 Json 输出中的基础对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13403692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com