简体   繁体   English

有没有办法检查 org.json java package 的 JSONObject 中是否存在密钥?

[英]is there a way to check for existence of a key in JSONObject of org.json java package?

Essentially its as the title says, I want to check if key "error" exists in the JSONObject but guarding it with try catch as a part of normal control flow seems to be a very bad and dirty thing to do, so surely it cant be the intended solution.本质上,正如标题所说,我想检查 JSONObject 中是否存在关键“错误”,但使用 try catch 作为正常控制流的一部分来保护它似乎是一件非常糟糕和肮脏的事情,所以肯定不能预期的解决方案。 But I cant think of any other way to do it.但我想不出任何其他方法来做到这一点。

There is a has method to check if the key exists有一个has方法来检查密钥是否存在

You can also use one of the opt methods so that you can get back a default value in case the key doesn't exist您还可以使用其中一种opt方法,以便在密钥不存在的情况下取回默认值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM