摘要:的官方类型是,文件扩展名是。值与值之间使用逗号分隔。通过以上两种结构可以表示各种复杂结构。对象示例对象由一系列无序的键值对组成。方法生成的字符串表示。对于基本类型如则会返回该类型近乎友好的值,详见续表。
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 optThe 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) |
什么是 JSON
JSONObject - Oracle
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/69272.html
摘要:发现环信的根据时间条件拉取历史消息接口已经停用就做了个通过导出聊天记录接口保存到数据库实体的功能分享一下大致思路通过环信的接口把前一天小时的数据压缩包下载到本地把下载后的文件解压读取处理写入到实体设置一个定时器定时执行通过环信接口拉取数据并 发现环信的根据时间条件拉取历史消息接口已经停用,就做了个通过导出聊天记录接口保存到数据库实体的功能,分享一下. 大致思路: 1.通过环信的接口,把...
阅读 885·2021-11-24 09:38
阅读 1107·2021-10-08 10:05
阅读 2603·2021-09-10 11:21
阅读 2816·2019-08-30 15:53
阅读 1843·2019-08-30 15:52
阅读 1984·2019-08-29 12:17
阅读 3433·2019-08-29 11:21
阅读 1624·2019-08-26 12:17