How to check if a json key exists?

后端 未结 13 2242
一整个雨季
一整个雨季 2020-11-27 11:38

So, I get some JSON values from the server but I don\'t know if there will be a particular field or not.

So like:

{ \"regatta_name\":\"ProbaRegatta\"         


        
13条回答
  •  我在风中等你
    2020-11-27 12:07

    just before read key check it like before read

    JSONObject json_obj=new JSONObject(yourjsonstr);
    if(!json_obj.isNull("club"))
    {
      //it's contain value to be read operation
    }
    else
    {
      //it's not contain key club or isnull so do this operation here
    }
    

    isNull function definition

    Returns true if this object has no mapping for name or
    if it has a mapping whose value is NULL. 
    

    official documentation below link for isNull function

    http://developer.android.com/reference/org/json/JSONObject.html#isNull(java.lang.String)

提交回复
热议问题