Java:Json具有键/字段 [英] Java: Json has key/field
问题描述
我想知道检测JSON字符串中是否存在键/字段的最简单方法.
I'd like to know the most simple way to detect if a key/field exists in a JSON String.
例如:
if(jsonObject(myJsonString).hasKey("myKey")){
}
我不想写太多.我目前使用的是最低限度的JSON,它似乎没有这样的功能.
I would not prefer to write a lot. I am currently using minimal JSON and it appears it does not have such a function.
答案: JSONObject jsonObj2 =新的JSONObject(message); if(jsonObj2.has("key"));
Answer: JSONObject jsonObj2 = new JSONObject(message); if(jsonObj2.has("key"));
推荐答案
不确定最小的JSON到底是什么意思,我个人认为org.json
包简单明了(即最小的开销).可以在此处找到.例如,org.json.JSONObject
类包含public boolean has(String key)
方法,该方法用于检查某个键是否存在.
Not sure what you mean exactly by minimal JSON, personally I find the org.json
package simple and straightforward (i.e. minimal overhead). It is found here. The org.json.JSONObject
class, for example, contains the public boolean has(String key)
method, which is used to check if a certain key exists.
这篇关于Java:Json具有键/字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!