How to check if a json key exists?

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

    I am just adding another thing, In case you just want to check whether anything is created in JSONObject or not you can use length(), because by default when JSONObject is initialized and no key is inserted, it just has empty braces {} and using has(String key) doesn't make any sense.

    So you can directly write if (jsonObject.length() > 0) and do your things.

    Happy learning!

提交回复
热议问题