How to check if a json key exists?

后端 未结 13 2239
一整个雨季
一整个雨季 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 12:22

    You can try this to check wether the key exists or not:

    JSONObject object = new JSONObject(jsonfile);
    if (object.containskey("key")) {
      object.get("key");
      //etc. etc.
    }
    

提交回复
热议问题