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

时间:2012-11-15 18:19:15

标签: java json jersey

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

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

}

我们正在使用泽西岛和这个对象:

     @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"
    }]

任何帮助?

1 个答案:

答案 0 :(得分:1)

您可以使用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"
    }]