成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Java JSONObject get or opt

CntChen / 2821人閱讀

摘要:的官方類型是,文件擴(kuò)展名是。值與值之間使用逗號分隔。通過以上兩種結(jié)構(gòu)可以表示各種復(fù)雜結(jié)構(gòu)。對象示例對象由一系列無序的鍵值對組成。方法生成的字符串表示。對于基本類型如則會返回該類型近乎友好的值,詳見續(xù)表。

JSON

JSON(JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式,它使得人們能夠輕易地閱讀和編寫,同時也方便機(jī)器進(jìn)行解析和生成。盡管 JSON 脫胎于 JavaScript 但其本身采用完全獨(dú)立于程序語言的文本格式,是理想的數(shù)據(jù)交換方式。JSON 的官方 MIME 類型是 application/json,文件擴(kuò)展名是 .json。

JSON 存在兩種結(jié)構(gòu):

對象,一個 JSON 對象以{(左括號)開始,}(右括號)結(jié)束,數(shù)據(jù)結(jié)構(gòu)為 {key:value, key:value,...} 的鍵值對,key 代表對象的屬性,value 代表對應(yīng)的屬性值,鍵與值中間包含一個:(冒號),多個鍵值對之間使用,(逗號)分隔。

數(shù)組,value(值)的有序集合。一個數(shù)組以[(左中括號)開始,](右中括號)結(jié)束。值與值之間使用,(逗號)分隔。

通過以上兩種結(jié)構(gòu)可以表示各種復(fù)雜結(jié)構(gòu)。

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 方法返回值時,如果找不到并不會拋出異常,而是返回友好的默認(rèn)值。

例如:

當(dāng)獲取 Object(JSONArray、JSONObject) 對象時候,如若 key(鍵)不存在或者值的類型不匹配,則默認(rèn)返回 null,因此只需檢查它是否為空,進(jìn)而繼續(xù)執(zhí)行函數(shù)的功能。

對于獲取 String 對象,如若沒有鍵,將返回一個空字符串“”,如若存在鍵值對但值并非 String 類型,則進(jìn)行類型轉(zhuǎn)換。

對于基本類型如 boolean、double、int、long 則會返回該類型近乎友好的值,詳見續(xù)表。同時存在包含默認(rèn)值(defaultValue)參數(shù)的 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.

續(xù)表:

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 參數(shù)的版本:

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

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/69272.html

相關(guān)文章

  • 環(huán)信聊天記錄保存到數(shù)據(jù)庫實體

    摘要:發(fā)現(xiàn)環(huán)信的根據(jù)時間條件拉取歷史消息接口已經(jīng)停用就做了個通過導(dǎo)出聊天記錄接口保存到數(shù)據(jù)庫實體的功能分享一下大致思路通過環(huán)信的接口把前一天小時的數(shù)據(jù)壓縮包下載到本地把下載后的文件解壓讀取處理寫入到實體設(shè)置一個定時器定時執(zhí)行通過環(huán)信接口拉取數(shù)據(jù)并 發(fā)現(xiàn)環(huán)信的根據(jù)時間條件拉取歷史消息接口已經(jīng)停用,就做了個通過導(dǎo)出聊天記錄接口保存到數(shù)據(jù)庫實體的功能,分享一下. 大致思路: 1.通過環(huán)信的接口,把...

    reclay 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<