2016-02-01 92 views
0

使用Retrofit2中的JacksonConverterFactory將我的json從服務器轉換爲對象。但是,因爲即使沒有領域,RealmList對象也不應該爲null。我嘗試過.getSerializationConfig()。withSerializationInclusion(JsonInclude.Include.NON_NULL);但看起來他仍然試圖向RealmList添加空值。傑克遜不會忽略json數組中的空對象

ObjectMapper mapper = new ObjectMapper();  mapper.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.NON_EMPTY); 
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 
... 
.addConverterFactory(JacksonConverterFactory.create(mapper) 

的JSON看起來像這樣

"items": [ 
    { 
     "id": 8941, 
     "size": 4 
     "childs": [ 
     null, 
     { 
      "id": 32134, 
      "count": 1 
     }, 
     null, 
     null 
     ] 
    }, 
    null, 
    { 
     "id": 8932, 
     "size": 20 
     "childs": [ 
     null, 
     null, 
     ... 
     ] 
    }, 
    { 
     "id": 48715, 
     "size": 20 
    }, 
    null 
    ] 

的logcat的:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: RealmList does not accept null values (through reference chain: java.util.ArrayList[11]->my.package.myobject["items"]->io.realm.RealmList[4]) 

回答

1

在使用您所提供JsonInclude.Include.NON_EMPTY片段。 你真的嘗試JsonInclude.Include.NON_NULL嗎?

+0

是的,我已經試過 – Fabian

+0

我也試過gson,但對於gson他做的是一樣的 – Fabian

+0

你看看[這裏](http://stackoverflow.com/a/30644536/3968213)? –