JSONObject的getBoolean(“error”)方法如何工作?

huangapple go评论59阅读模式
英文:

How JSONObject method getBoolean("error") works?

问题

我有以下包含错误检查表达式的代码:

JSONObject obj = new JSONObject(response);
if (!obj.getBoolean("error"))

我想知道 getBoolean(String name) 是如何工作的,它是否只会在整个响应主体中搜索 "error" 字符串,并返回 TRUE 或 FALSE,因为除了 "error" 之外,我们没有提供任何参数,或者还有更深层次的东西吗?
在返回的对象中,"error" 是一个 还是一个

英文:

I have following code which includes error checking expression:

JSONObject obj = new JSONObject(response);
if (!obj.getBoolean("error"))

I am wondering, how getBoolean(String name) works, will it search just "error" string in whole response body & return TRUE or FALSE as we did not provide any arguments except "error" or there is something deeper than this?
Is "error" a key or a value in the returned object?

答案1

得分: 1

"error" 是一个键。

JSONObject 代表 JSON 中的单个对象,JSONObject.getBoolean(String key) 方法会在该对象中查找具有给定键的键值对。它仅查看该对象的第一层,不会深入查找。

英文:

"error" is a key.

JSONObject represents a single object in JSON, and the JSONObject.getBoolean(String key) method looks for a key-value pair with the given key in that object. It look only at the first level of that one object, not deeper.

答案2

得分: 0

obj.getBoolean("error") 中的 error 是键名。getBoolean 方法寻找 error 键名,如果存在且为布尔类型,则返回其值;否则会抛出 JSONException。可使用 optBoolean 替代,它会返回 false 以处理异常情况,或者返回指定的备用值。

英文:

error in obj.getBoolean("error") is the key name. getBoolean method looks for error key name and returns its value if it exists and is of Boolean type else throws JSONException. Use optBoolean instead which returns false for exception or fallback value if specified.

huangapple
  • 本文由 发表于 2020年4月11日 00:27:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61144481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定