欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JSON中optString和getString方法的區(qū)別

 更新時(shí)間:2017年07月07日 15:18:24   投稿:mrr  
optString方法會在對應(yīng)的key中的值不存在的時(shí)候返回一個(gè)空字符串,但是getString會拋一個(gè)JSONException 。下面通過一段代碼給大家介紹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)文章

最新評論