JSON中optString和getString方法的區(qū)別
optString方法會在對應(yīng)的key中的值不存在的時(shí)候返回一個(gè)空字符串,但是getString會拋一個(gè)JSONException 。
/** * Returns the value mapped by {@code name} if it exists, coercing it if * necessary, or throws if no such mapping exists. * * @throws JSONException if no such mapping exists. */ public String getString(String name) throws JSONException { Object object = get(name); String result = JSON.toString(object); if (result == null) { throw JSON.typeMismatch(name, object, "String"); } return result; } /** * Returns the value mapped by {@code name} if it exists, coercing it if * necessary, or the empty string if no such mapping exists. */ public String optString(String name) { return optString(name, ""); }
以上所述是小編給大家介紹的JSON中optString和getString方法的區(qū)別,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解Android TextView屬性ellipsize多行失效的解決思路
這篇文章主要介紹了Android TextView屬性ellipsize多行失效的解決思路,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07android surfaceView實(shí)現(xiàn)播放視頻功能
這篇文章主要為大家詳細(xì)介紹了android surfaceView實(shí)現(xiàn)播放視頻功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05使用Jetpack Compose實(shí)現(xiàn)翻轉(zhuǎn)卡片效果流程詳解
Jetpack Compose 是一款基于 Kotlin 的聲明式 UI 工具包,可以方便地創(chuàng)建漂亮的用戶界面。使用 Compose 的動畫 API 和可繪制 API,可以輕松實(shí)現(xiàn)翻轉(zhuǎn)卡片效果。通過設(shè)置旋轉(zhuǎn)角度和透明度等屬性,可以使卡片沿著 Y 軸翻轉(zhuǎn),并實(shí)現(xiàn)翻頁效果2023-05-05Android WebView實(shí)現(xiàn)截長圖功能
這篇文章主要為大家詳細(xì)介紹了Android截長圖的一種實(shí)現(xiàn)方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android實(shí)現(xiàn)通訊錄效果——獲取手機(jī)號碼和姓名
這篇文章主要介紹了Android實(shí)現(xiàn)通訊錄效果——獲取手機(jī)號碼和姓名的相關(guān)資料,需要的朋友可以參考下2016-03-03Android 啟動 Service(startservice和bindservice) 兩種方式的區(qū)別
andrid service 兩種啟動方式:第一種startservice,第二種bindservice,大家對這兩種啟動方式的區(qū)別了解嗎,下面跟著小編一起學(xué)習(xí)吧2015-11-11詳解Android冷啟動實(shí)現(xiàn)APP秒開的方法
這篇文章給大家介紹的是Android冷啟動實(shí)現(xiàn)APP秒開的方法,對大家日常開發(fā)APP還是很實(shí)用的,有需要的可以參考借鑒。2016-08-08