Java JSONObject get or opt
JSON
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,它使得人们能够轻易地阅读和编写,同时也方便机器进行解析和生成。尽管 JSON 脱胎于 JavaScript 但其本身采用完全独立于程序语言的文本格式,是理想的数据交换方式。JSON 的官方 MIME 类型是 application/json,文件扩展名是 .json。
JSON 存在两种结构:
- 对象,一个 JSON 对象以
{(左括号)开始,}(右括号)结束,数据结构为{key:value, key:value,...}的键值对,key代表对象的属性,value代表对应的属性值,键与值中间包含一个:(冒号),多个键值对之间使用,(逗号)分隔。 - 数组,
value(值)的有序集合。一个数组以[(左中括号)开始,](右中括号)结束。值与值之间使用,(逗号)分隔。
通过以上两种结构可以表示各种复杂结构。
JavaScript JSON 对象示例:
myObj = {
"name":"John",
"age":30,
"cars": [
{ "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
{ "name":"BMW", "models":[ "320", "X3", "X5" ] },
{ "name":"Fiat", "models":[ "500", "Panda" ] }
]
}
JSONObject
JSONObject 对象由一系列无序的键值对组成。具有 get 和 opt 方法用于按 key(键)访问 value(值),同时提供 put 方法用于按 key(键)添加或替换 value(值)。toString() 方法生成 JSON 的字符串表示。
Java 中值可以是以下任何类型:Boolean, JSONArray, JSONObject, Number, String, JSONObject.NULL 对象。
get or opt
The opt methods differ from the get methods in that they do not throw. Instead, they return a specified value, such as null.
使用 get 方法返回值时,如果找不到就抛出一个异常。需要我们使用 try catch 语句或者 throw。
使用 opt 方法返回值时,如果找不到并不会抛出异常,而是返回友好的默认值。
例如:
- 当获取 Object(JSONArray、JSONObject) 对象时候,如若
key(键)不存在或者值的类型不匹配,则默认返回null,因此只需检查它是否为空,进而继续执行函数的功能。 - 对于获取 String 对象,如若没有键,将返回一个空字符串“”,如若存在键值对但值并非 String 类型,则进行类型转换。
- 对于基本类型如 boolean、double、int、long 则会返回该类型近乎友好的值,详见续表。同时存在包含默认值(defaultValue)参数的 opt 方法重载版本。
一言以蔽之,对于获取 JSON 中可选的值(键可有可无,值可对可错)推荐使用 opt 方法。
| Method | Description |
|---|---|
| optJSONArray | Get an optional JSONArray associated with a key. It returns null if there is no such key, or if its value is not a JSONArray. |
| optJSONObject | Get an optional JSONObject associated with a key. It returns null if there is no such key, or if its value is not a JSONObject. |
| optString | Get an optional string associated with a key. It returns an empty string if there is no such key. If the value is not a string and is not null, then it is converted to a string. |
续表:
| Method | Description |
|---|---|
| optBoolean | Get an optional boolean associated with a key. It returns false if there is no such key, or if the value is not Boolean.TRUE or the String "true". |
| optDouble | Get an optional double associated with a key, or NaN if there is no such key or if its value is not a number. If the value is a string, an attempt will be made to evaluate it as a number. |
| optInt | Get an optional int value associated with a key, or zero if there is no such key or if the value is not a number. If the value is a string, an attempt will be made to evaluate it as a number. |
| optLong | Get an optional long value associated with a key, or zero if there is no such key or if the value is not a number. If the value is a string, an attempt will be made to evaluate it as a number. |
上述 opt 方法还提供带有 defaultValue 参数的版本:
| Type | Method |
|---|---|
| boolean | optBoolean(String key, boolean defaultValue) |
| double | optDouble(String key, double defaultValue) |
| int | optInt(String key, int defaultValue) |
| long | optLong(String key, long defaultValue) |
参考
转载自:https://segmentfault.com/a/1190000014709644