-2

I have a json where I want its values down in a map, unless that value is NULL, if it is I just want an empty string instead.

I've tried a few different view of telling what the value I get out of the json is, but none of them seems to be able to tell the value is null.

if (!isNull(json.get("id"))) {
   orderDataMap.put("id", json.get("id"));
} else {
   orderDataMap.put("id", "");
}

if (!isNull(json.get("sr"))) {
   orderDataMap.put("sr", json.get("sr"));
} else {
   orderDataMap.put("sr", "");
}
if (!isNull(json.get("systemOrderId"))) {
   orderDataMap.put("systemOrderId", json.get("systemOrderId"));
} else {
   orderDataMap.put("systemOrderId", "");
}

in the if statement I've also tried using json.get("id") != NULL as well as json.get("id").equals(null)

Here's how it looks while debugging: debugger

Mahmood Afzalzadeh
  • 180
  • 1
  • 2
  • 20
Mimeer
  • 81
  • 1
  • 9

1 Answers1

2

Use JSONObject.isNull to check if a value is not present or is null.

if (json.isNull("id")) {
    orderDataMap.put("id", json.get("id"));
    } else {
    orderDataMap.put("id", "");
}
Martin'sRun
  • 522
  • 3
  • 11