Java:Json具有键/字段 [英] Java: Json has key/field

查看:111
本文介绍了Java:Json具有键/字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道检测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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆