How to check if a json key exists?

后端 未结 13 2077
一整个雨季
一整个雨季 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:26

    You can use has

    public boolean has(String key)
    

    Determine if the JSONObject contains a specific key.

    Example

    JSONObject JsonObj = new JSONObject(Your_API_STRING); //JSONObject is an unordered collection of name/value pairs
    
    if (JsonObj.has("address")) { 
        //Checking address Key Present or not
        String get_address = JsonObj .getString("address"); // Present Key
    }
    else {
        //Do Your Staff
    }
    
    0 讨论(0)
提交回复
热议问题