user1256477
user1256477

Reputation: 11201

json and null value

I can't understand why this code is crashing, I have this response from my server:

    [{
        "parking1": "Plaza de la Estacion",
        "parking2": "",
        "takeDate": "2012-12-11 11:00:48",
        "returnDate": null,
        "time": null,
        "cost": "0.00"
    }]

I convert this into a JSONObject and check if returnDate is null:

JSONObject json_data = jArray.getJSONObject(0);
if (json_data.getString("returnDate") == null) {
}

But this condition is never true. How should it be checked if null?

Upvotes: 1

Views: 113

Answers (2)

Faizan
Faizan

Reputation: 3512

Try

JSONObject json_data = jArray.getJSONObject(0);
if (json_data.opt("returnDate") == null) {
}

Upvotes: 0

Nermeen
Nermeen

Reputation: 15973

You can check if a json entry is null using:

if(json_data.isNull("returnDate")) {
}

Upvotes: 3

Related Questions