软件编程
位置:首页>> 软件编程>> Android编程>> JSON中optString和getString方法的区别

JSON中optString和getString方法的区别

作者:mrr  发布时间:2021-09-06 03:46:14 

标签:json,optString,getString

optString方法会在对应的key中的值不存在的时候返回一个空字符串,但是getString会抛一个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方法的区别网站的支持!

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com