我想檢查我的數據是否有空值。 JSON格式的數據低於:在嵌套數據(Java)中找到空值和空字符串
"{
"totalAmount" : 20000,
"items" : [
{
"id" : "9fd91eaf-2b9a-4bae-9692-4222f85e868",
"name" : "X hotel",
"itemType" : 0,
"orderCount" : 2,
"amount" : 5000
},
{
"id" : "9fd91eaf-2b9a-4bae-9692-4222f85e869",
"name": "Special Dinner",
"itemType" : 1,
"orderCount" : 1,
"amount" : 10000
}
]
}"
這是我的檢查邏輯。
class NullCheck {
public checkNull (Itinerary itinerary)
Object nu = null;
if (itinerary == null) {
return true;
} else if (itinerary.getTotalAmount() == null || itinerary.getItems() == null) {
return true;
} else if (itinerary.getItems().contains(null)) {
return true;
} else if (itinerary.getItems().stream().flatMap //
(item -> Arrays.stream(item.getClass().getDeclaredFields()))
.map(field -> { field.setAccessible(true); return field.get(nu) == null; })) {
return true;
}
}
然而,IlligalAccessException在field.get(NU)扔了,我想不通爲什麼我的代碼不能很好地工作。 當我閱讀java文檔時,它在語法上似乎是正確的。 我可以問我的代碼哪部分失敗? 此外,某些json格式數據轉換爲String
類型的對象,因此我還需要檢查string
是否爲空string
。 有沒有簡單的方法來做到這一點?