How to check if JSONArray Element is null

前端 未结 3 466
渐次进展
渐次进展 2020-12-11 02:25

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:

jsonObj         


        
相关标签:
3条回答
  • 2020-12-11 02:50

    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));
    
    0 讨论(0)
  • 2020-12-11 03:06

    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
    }
    
    0 讨论(0)
  • 2020-12-11 03:10

    I guess json passes null values as strings, so you can't check null as a java element. Instead treat the null value as a string as check this way:

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

    I have updated the code snippet in the original question to a working version.

    0 讨论(0)
提交回复
热议问题