suwisan prommuak
suwisan prommuak

Reputation: 23

Set null when JSON Object doesn't have a key for value

I have an object which needs 4 parameters to set the values from a JSONObject. But, when I call get(String name) from JSONObject it has 3 values, so that throws a JSONException. Here is an example:

"JSON":{
    "aaa":"111",
    "bbb":"111",
    "ddd":"111"
}

How can set null for a value if the JSON key is non-existant.. not using GSON

Upvotes: 2

Views: 1037

Answers (1)

Christian
Christian

Reputation: 4641

like this:

if (jsonObject.has("aaa") {
    this.aaa = jsonObject.getString("aaa");
}

Upvotes: 1

Related Questions