Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if JSONArray Element is null

Tags:

java

json

arrays

I can't figure out how to determine is an element that lives inside a json array is null. To check if the jsonObject itself is null, you simply use:

jsonObject.isNullObject();

But when the object is an array and I want to check if one of the elements of that array is null, this does not work:

jsonArray.get(i).get("valueThatIsNull") == null;

There is also no isNull method available on elements of an array. How do I check if values inside a jsonarray are null? It might help to know that I am passing over a null object from javascript. Maybe null does not mean the same thing in java when it is passed from javascript in json format, but I have also tried putting parentheses around the null and it still does not work.

I am posting some actual source code to help make this clearer. The jsonObject is a part of the jsonArray and the object has multiple values because it iself is an object.

JSONObject mapItem = jsonArray.getJSONObject(i);
int id = mapItem.has("id") ? mapItem.getInt("id") : -1;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = null;
Date sqlDate = null;
if(mapItem.has("date")) {
    String dateStr = mapItem.getString("date");
    if(!dateStr.equals("null")) {
    date = dateFormat.parse(mapItem.getString("date").substring(0, 10)); //Convert javascript date string to java.
    sqlDate = new Date(date.getTime());
}
like image 955
ryandlf Avatar asked Sep 03 '12 17:09

ryandlf


People also ask

Can JSONArray be null?

Returns: A JSONArray value, or null if the index has no value, or if the value is not a JSONArray.

How do you check if a JSON object is empty or not?

Using JSON. If we stringify the object and the result is simply an opening and closing bracket, we know the object is empty.

How do I read JSONArray?

String value = (String) jsonObject. get("key_name"); Just like other element retrieve the json array using the get() method into the JSONArray object.

How do I check if an object is JSONArray or JSONObject?

You can check the character at the first position of the String (after trimming away whitespace, as it is allowed in valid JSON). If it is a { , you are dealing with a JSONObject , if it is a [ , you are dealing with a JSONArray . If you are dealing with JSON (an Object ), then you can do an instanceof check.


2 Answers

Try .isNull():

For your example:

if(!mapItem.isNull("date")) {
    //Value is not null
}

However, to answer the title of this question, "how to tell if a JSONArray element is null", use .equals()

So, to check if index 1 is null:

if (!jsonArray.get(1).equals(null)) {
    //jsonArray[1] is not null
}
like image 51
Peter Ajtai Avatar answered Sep 18 '22 11:09

Peter Ajtai


try JSONArray's method

public boolean isNull (int index)

In fact, it uses "null" string comparing to the content

JSONObject.NULL.equals(this.opt(index));
like image 30
mianlaoshu Avatar answered Sep 18 '22 11:09

mianlaoshu