2016-07-13 55 views
1

我正在处理与服务器通信并将数据分别发送到网站的应用程序,并从该网站接收数据。 数据大约是可选的5个时间表。将对象转换为JSON字符串与杰克逊

例如,如果设定了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); 

字符串为空,但是在返回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}}} 

回答

1

尝试使用com.google.gson.Gson如果任何属性为空解析器

String jsonInString = new Gson().toJson(yourObject); 

结果:

{ 
"priorityOne":{}, 
"priorityTwo":{}, 
    ... 
} 
+4

这似乎是问题是试图使用杰克逊。请尊重那个意图。 –

0

是否设置序列化包含到NON_NULL给你你想要什么?

public String toJsonObject() throws IOException{ 

    String jsonInString = ""; 
    ObjectMapper mapper = new ObjectMapper(); 
    //Object to JSON in String 
    try { 
     // Will only include non null objects 
     mapper.setSerializationInclusion(Inclusion.NON_NULL); 
     jsonInString = mapper.writeValueAsString(this); 
    } catch (JsonProcessingException e) { 
     e.printStackTrace(); 
    } 

    return jsonInString; 
} 
+0

不幸的是我收到了同样的结果。即使使用Inclusion.NON_NULL,问题仍然存在。 – ruru

+0

嘿:
你能清楚地列出你想要在你的输出中看到什么吗?以及“PriorityResponse”对象中5个“优先级”实例的状态。 –

+0

我用输出编辑我的问题。 – ruru