JSON中getString() 和optString()

The difference is that optString returns the empty string ("") if the key you specify doesn't exist.getString on the other hand throws a JSONException. Use getString if it's an error for the data to be missing, or optString if you're not sure if it will be there.

optString和getString的区别是,如果你需要的String 特殊字不存在的话,optString会返回一个“”;而getString则会抛出异常;所以如果在你不确定的情况下尽量使用optString,因为这时候使用getString需要你确定data已经丢失;

你可能感兴趣的:(JSON中getString() 和optString())