Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i handle null value by using JSonobject.getInt function?

Tags:

json

android

I'm trying to get values from Json object and I have a problem. I'm using getint function to get value but the value is null and getint function gving error.

How can I solve this problem?

Code :

firmInfo.setFirmID(object.getInt(Constants.FirmID));

Thanks.

like image 281
mertaydin Avatar asked Dec 28 '12 09:12

mertaydin


People also ask

Can JSON object have null values?

JSON has two types of null value After reaching out it became clear to me that the issue here wasn't specific to Go, but rather a shortcoming of JSON used in conjunction with any typed language. In JSON there are effectively two types of null .

How check value is null in JSON?

To check null in JavaScript, use triple equals operator(===) or Object is() method. If you want to use Object.is() method then you two arguments. 1) Pass your variable value with a null value. 2) The null value itself.

How do you handle a null response in Java?

Using simple java rules. Check if the array is empty, if the array does not exist and you try to get it, it just returns null. Just handle it. Don't continue parsing if you know its going to fail.


1 Answers

Assuming that object is of type JSONObject you can use

object.optInt(Constants.FirmID)

or

object.optInt(Constants.FirmID, defaultValue)
like image 70
Henry Avatar answered Oct 24 '22 02:10

Henry