JSON中getString() 和optString()

来源:互联网 发布:做编程一月工资多少钱 编辑:程序博客网 时间:2024/04/23 18:12

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已经丢失;

0 0