我希望在根据条件作为输入传递给API时从JSON对象中排除一个值。即某些情况下,部分密钥:不需要值,但对于某些密钥:值是需要的。
输入JSON如下
{
"customerNumber" : "6634374019",
"ea" : "",
"source" : "Test",
"dataCentre" : "US",
"requestId" : "CSP-QA-1574"
}
和上面的类如下
public class order
{
@JsonProperty(value="customerNumber")
public String customerNumber;
@JsonProperty(value="ea")
public String ea;
@JsonProperty(value="source")
public String source;
@JsonProperty(value="dataCentre")
public String dataCentre;
@JsonProperty(value="requestId")
public String requestId;
//constructor for assiging data
//getters and setters
}
任何人都可以帮助我。我不想通过" ea"对于某些场景,在JSON输入中键:值,在某些情况下需要通过。如何实现这一点。
提前thnaks答案 0 :(得分:0)
使用:
@JsonIgnore
快乐。
干杯
答案 1 :(得分:0)
如果我必须这样做,那么我会采用稍微不同的方法。
JsonObject gsonObject=new JsonObject();
gsonObject.addProperty("ea","value");
就像有条件的addProperty()。 因为我没有足够的代表所以我把它当作答案而不是评论
答案 2 :(得分:0)
如果要忽略任何未传递的值,可以在类级别使用@JsonIgnoreProperties。如果要忽略单个值,可以在成员级别使用@JsonIgnore。
@JsonIgnoreProperties({"Ignore 1", "Ignore 2"})
class Sample {
或
class Sample {
private String xyz;
@JsonIgnore
private String ignorethis;