Code example for JSONObject

Methods: optBoolean, optString

0
        } 
    } 
 
    @SuppressWarnings("nls") 
    private void addPreferenceFromJSON(JSONObject obj) {
        String type = obj.optString("type", null);
        String key = obj.optString("key", null);
        String title = obj.optString("title", null);
        if (type == null || key == null || title == null)
            return; 
 
        Preference pref = null;
        if ("bool".equals(type)) { // We can add other types we want to support and handle the preference construction here
            pref = new CheckBoxPreference(this);
            pref.setKey(key);
            pref.setTitle(title);
            pref.setDefaultValue(Preferences.getBoolean(key, false));
        } 
 
        if (pref == null)
            return; 
 
        if (obj.optBoolean("restart")) {
            pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
                @Override 
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    setResult(RESULT_OK);
                    return true;