How to check if a json key exists?

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

    Json has a method called containsKey().

    You can use it to check if a certain key is contained in the Json set.

    File jsonInputFile = new File("jsonFile.json"); 
    InputStream is = new FileInputStream(jsonInputFile);
    JsonReader reader = Json.createReader(is);
    JsonObject frameObj = reader.readObject();
    reader.close();
    
    if frameObj.containsKey("person") {
          //Do stuff
    }
    

提交回复
热议问题