如何检查是否一个JSON键是否存在?是否存在、JSON

2023-09-05 11:14:08 作者:人生若只如初見

所以,我从服务器获取一些JSON的价值观,但我不知道是否会有某一领域或没有。

So i get some json values from the server but i dont know if there will be a particular field or not.

所以像:

{"regatta_name":"ProbaRegatta",
"country":"Congo",
"status":"invited"}

有时候,会有一个额外的字段,如:

And sometimes, there will be an extra field like:

{"regatta_name":"ProbaRegatta",
"country":"Congo",
"status":"invited",
"club":"somevalue"}

所以,我想检查code如果有一个名为俱乐部,所以在分析我不会得到 org.json.JSONException领域:为俱乐部无值

推荐答案

的JSONObject类有一个方法命名为有:

JSONObject class has a method named "has":

http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)

返回true,如果这个对象的名称映射。映射可以为NULL。

Returns true if this object has a mapping for name. The mapping may be NULL.