我正在开发一个与服务器通信的应用程序,并分别向网站发送数据,从该网站接收数据。 数据大约是5个可选的时间表。
例如如果设置了3个时间表,并且我想取消其中一个时间表,它应该只向网站发送2个时间表。这是一个问题。
我分析了我的代码,并得出结论,问题出在将对象转换为JSON字符串的方法中。
以下是代码:
public class PriorityResponse {
@JsonProperty("priority1")
public Priority priorityOne;
@JsonProperty("priority2")
public Priority priorityTwo;
@JsonProperty("priority3")
public Priority priorityThree;
@JsonProperty("priority4")
public Priority priorityFour;
@JsonProperty("priority5")
public Priority priorityFive;
public Priority getPriorityOne() {
return priorityOne;
}
public Priority getPriorityTwo() {
return priorityTwo;
}
public Priority getPriorityThree() {
return priorityThree;
}
public Priority getPriorityFour() {
return priorityFour;
}
public Priority getPriorityFive() {
return priorityFive;
}
public String toJsonObject(){
String jsonInString = "";
ObjectMapper mapper = new ObjectMapper();
//Object to JSON in String
try {
jsonInString = mapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return jsonInString;
}
}
在这一行
jsonInString = mapper.writeValueAsString(this);
字符串为null,但是当返回jsonInString时,发送3时间表,而不是2。 如果我进行其他更改,它会发送正确的数据,问题仅在我想取消选择一个或多个时间表时。
当我取消选择Priority4:
时,输出应该是这样的{"priority5":{"number":null,"timeOut":null,"timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":false,"op":true},"fri":true,"mon":true,"sat":true,"sun":true,"thu":true,"tue":true,"wed":true}},"priority4":{"number":null,"timeOut":null,"timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":false,"op":true},"fri":true,"mon":true,"sat":true,"sun":true,"thu":true,"tue":true,"wed":true}},"priority1":{"number":"1234561231","timeOut":"9","timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":false,"op":true},"fri":true,"mon":true,"sat":false,"sun":false,"thu":true,"tue":true,"wed":true}},"priority3":{"number":"21515211545","timeOut":"","timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":true,"op":false},"fri":false,"mon":true,"sat":false,"sun":false,"thu":true,"tue":true,"wed":false}},"priority2":{"number":"789123156421","timeOut":"","timeTable":{"time":{"fh":"04","fm":"02","th":"13","tm":"07","cl":false,"op":false},"fri":true,"mon":true,"sat":true,"sun":true,"thu":true,"tue":true,"wed":true}}}
但是,看起来,当我取消选择一个优先级时,JSON字符串不会刷新,因为发送的优先级与我获得的优先级相同。 这是发送的内容:
{"priority5":{"number":null,"timeOut":null,"timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":false,"op":true},"fri":true,"mon":true,"sat":true,"sun":true,"thu":true,"tue":true,"wed":true}},"priority4":{"number":"12233456545","timeOut":"6","timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":false,"op":true},"fri":true,"mon":true,"sat":false,"sun":false,"thu":true,"tue":true,"wed":true}},"priority1":{"number":"1234561231","timeOut":"9","timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":false,"op":true},"fri":true,"mon":true,"sat":false,"sun":false,"thu":true,"tue":true,"wed":true}},"priority3":{"number":"21515211545","timeOut":"5","timeTable":{"time":{"fh":null,"fm":null,"th":null,"tm":null,"cl":true,"op":false},"fri":false,"mon":true,"sat":false,"sun":false,"thu":true,"tue":true,"wed":false}},"priority2":{"number":"789123156421","timeOut":"5","timeTable":{"time":{"fh":"04","fm":"02","th":"13","tm":"07","cl":false,"op":false},"fri":true,"mon":true,"sat":true,"sun":true,"thu":true,"tue":true,"wed":true}}}
答案 0 :(得分:1)
尝试使用com.google.gson.Gson如果任何属性为null,则解析器为
String jsonInString = new Gson().toJson(yourObject);
结果:
{
"priorityOne":{},
"priorityTwo":{},
...
}
答案 1 :(得分:0)
将serializationInclusion设置为NON_NULL会为您提供所需内容吗?
"name"