首页 > Web开发 > 详细

org.json.JSONObject的getString和optString使用注意事项

时间:2019-04-16 18:44:44      阅读:302      评论:0      收藏:0      [点我收藏+]

结论:org.json.JSONObject的getString如果取不到对应的key会抛出异常,optString则不会

/**
     * 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, "");
    }

技术分享图片

技术分享图片

 

org.json.JSONObject的getString和optString使用注意事项

原文:https://www.cnblogs.com/tc310/p/10718993.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!