Example usage for org.json JSONObject JSONObject

List of usage examples for org.json JSONObject JSONObject

Introduction

In this page you can find the example usage for org.json JSONObject JSONObject.

Prototype

public JSONObject(String source) throws JSONException 

Source Link

Document

Construct a JSONObject from a source JSON text string.

Usage

From source file:com.orange.oidc.secproxy_service.Token.java

public void fromToken(String token) {
    reset();/*  ww  w  .ja v  a 2s.com*/
    if (token == null)
        return;

    try {
        JSONObject jObject = null;

        // try token as is
        try {
            jObject = new JSONObject(token);
        } catch (Exception e) {
        }

        // try to decode JWT
        if (jObject == null) {
            String ds = getJSON(token);
            if (ds != null)
                jObject = new JSONObject(ds);
        }

        if (jObject != null) {
            JSONArray names = jObject.names();
            if (names != null) {
                for (int j = 0; j < names.length(); j++) {
                    String name = names.getString(j);
                    // Log.d("Token",name);
                    setField(name, jObject.get(name));
                }
            }
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:cz.muni.fi.japanesedictionary.entity.JapaneseCharacter.java

/**
 * Takes json string and parses it to map of dictionary references.
 * /*from   w  w w .j  av  a 2  s .c o m*/
 * @param jsonString - JSON string to be parsed
 */
public void parseDicRef(String jsonString) {
    if (jsonString == null || jsonString.length() < 1) {
        return;
    }
    Map<String, String> dicRefTemp = new HashMap<>();
    JSONObject dicRefJson;
    try {
        dicRefJson = new JSONObject(jsonString);
    } catch (JSONException e) {
        Log.w(LOG_TAG, "getting parseJapaneseKeb()  initial expression failed: " + e.toString());
        return;
    }

    Iterator<?> keys = dicRefJson.keys();
    while (keys.hasNext()) {
        String key = (String) keys.next();
        String value;
        try {
            value = dicRefJson.getString(key);
            if (key != null && value != null) {
                dicRefTemp.put(key, value);
            }
        } catch (JSONException e) {
            Log.w(LOG_TAG, "parsing dicRef failed");
        }
    }
    if (dicRefTemp.size() > 0) {
        for (String key : dicRefTemp.keySet()) {
            addDicRef(key, dicRefTemp.get(key));
        }
    }

}

From source file:cz.muni.fi.japanesedictionary.entity.JapaneseCharacter.java

/**
 *   Adds JapaneseCharater to given bundle.
 * //  www .  ja  va  2s . com
 * @param bundle - bundle in which character should be saved
 *              - in case of null empty bundle is created
 * @return Bundle returns bundle which contains CharacterInfo
 */
public Bundle createBundleFromJapaneseCharacter(Bundle bundle) {
    if (bundle == null) {
        bundle = new Bundle();
    }
    if (this.getLiteral() != null && this.getLiteral().length() > 0) {
        bundle.putString(SAVE_CHARACTER_LITERAL, this.getLiteral());
    }
    if (this.getRadicalClassic() != 0) {
        bundle.putInt(SAVE_CHARACTER_RADICAL, this.getRadicalClassic());
    }
    if (this.getGrade() != 0) {
        bundle.putInt(SAVE_CHARACTER_GRADE, this.getGrade());
    }
    if (this.getStrokeCount() != 0) {
        bundle.putInt(SAVE_CHARACTER_STROKE_COUNT, this.getStrokeCount());
    }
    if (this.getSkip() != null && this.getSkip().length() > 0) {
        bundle.putString(SAVE_CHARACTER_SKIP, this.getSkip());
    }
    if (this.getDicRef() != null && this.getDicRef().size() > 0) {
        bundle.putString(SAVE_CHARACTER_DIC_REF, (new JSONObject(this.getDicRef()).toString()));
    }
    if (this.getRmGroupJaOn() != null && this.getRmGroupJaOn().size() > 0) {
        bundle.putString(SAVE_CHARACTER_JA_ON, (new JSONArray(this.getRmGroupJaOn())).toString());
    }
    if (this.getRmGroupJaKun() != null && this.getRmGroupJaKun().size() > 0) {
        bundle.putString(SAVE_CHARACTER_JA_KUN, (new JSONArray(this.getRmGroupJaKun())).toString());
    }
    if (this.getMeaningEnglish() != null && this.getMeaningEnglish().size() > 0) {
        bundle.putString(SAVE_CHARACTER_ENGLISH, (new JSONArray(this.getMeaningEnglish())).toString());
    }
    if (this.getMeaningFrench() != null && this.getMeaningFrench().size() > 0) {
        bundle.putString(SAVE_CHARACTER_FRENCH, (new JSONArray(this.getMeaningFrench())).toString());
    }
    if (this.getMeaningDutch() != null && this.getMeaningDutch().size() > 0) {
        bundle.putString(SAVE_CHARACTER_DUTCH, (new JSONArray(this.getMeaningDutch())).toString());
    }
    if (this.getMeaningGerman() != null && this.getMeaningGerman().size() > 0) {
        bundle.putString(SAVE_CHARACTER_GERMAN, (new JSONArray(this.getMeaningGerman())).toString());
    }
    if (this.getMeaningRussian() != null && this.getMeaningRussian().size() > 0) {
        bundle.putString(SAVE_CHARACTER_RUSSIAN, (new JSONArray(this.getMeaningRussian())).toString());
    }
    if (this.getNanori() != null && this.getNanori().size() > 0) {
        bundle.putString(SAVE_CHARACTER_NANORI, (new JSONArray(this.getNanori())).toString());
    }

    return bundle;
}

From source file:com.extremeboredom.wordattack.utils.JSONUtils.java

/**
 * get Long from jsonData/*from   w  w  w .  java  2  s. c  o m*/
 *
 * @param jsonData
 * @param key
 * @param defaultValue
 * @return <ul>
 * <li>if jsonObject is null, return defaultValue</li>
 * <li>if jsonData {@link JSONObject#JSONObject(String)} exception, return defaultValue</li>
 * <li>return {@link JSONUtils#getLong(JSONObject, String, Long)}</li>
 * </ul>
 */
public static Long getLong(String jsonData, String key, Long defaultValue) {
    if (StringUtils.isEmpty(jsonData)) {
        return defaultValue;
    }

    try {
        JSONObject jsonObject = new JSONObject(jsonData);
        return getLong(jsonObject, key, defaultValue);
    } catch (JSONException e) {
        if (isPrintException) {
            e.printStackTrace();
        }
        return defaultValue;
    }
}

From source file:com.extremeboredom.wordattack.utils.JSONUtils.java

/**
 * get Int from jsonData/*  w  w  w . ja v a2s . c o  m*/
 *
 * @param jsonData
 * @param key
 * @param defaultValue
 * @return <ul>
 * <li>if jsonObject is null, return defaultValue</li>
 * <li>if jsonData {@link JSONObject#JSONObject(String)} exception, return defaultValue</li>
 * <li>return {@link JSONUtils#getInt(JSONObject, String, Integer)}</li>
 * </ul>
 */
public static Integer getInt(String jsonData, String key, Integer defaultValue) {
    if (StringUtils.isEmpty(jsonData)) {
        return defaultValue;
    }

    try {
        JSONObject jsonObject = new JSONObject(jsonData);
        return getInt(jsonObject, key, defaultValue);
    } catch (JSONException e) {
        if (isPrintException) {
            e.printStackTrace();
        }
        return defaultValue;
    }
}

From source file:com.extremeboredom.wordattack.utils.JSONUtils.java

/**
 * get Double from jsonData//from w  ww.j  av  a2  s  .c  om
 *
 * @param jsonData
 * @param key
 * @param defaultValue
 * @return <ul>
 * <li>if jsonObject is null, return defaultValue</li>
 * <li>if jsonData {@link JSONObject#JSONObject(String)} exception, return defaultValue</li>
 * <li>return {@link JSONUtils#getDouble(JSONObject, String, Double)}</li>
 * </ul>
 */
public static Double getDouble(String jsonData, String key, Double defaultValue) {
    if (StringUtils.isEmpty(jsonData)) {
        return defaultValue;
    }

    try {
        JSONObject jsonObject = new JSONObject(jsonData);
        return getDouble(jsonObject, key, defaultValue);
    } catch (JSONException e) {
        if (isPrintException) {
            e.printStackTrace();
        }
        return defaultValue;
    }
}

From source file:com.extremeboredom.wordattack.utils.JSONUtils.java

/**
 * get String from jsonData//w w  w.ja  va 2 s .  c om
 *
 * @param jsonData
 * @param key
 * @param defaultValue
 * @return <ul>
 * <li>if jsonObject is null, return defaultValue</li>
 * <li>if jsonData {@link JSONObject#JSONObject(String)} exception, return defaultValue</li>
 * <li>return {@link JSONUtils#getString(JSONObject, String, String)}</li>
 * </ul>
 */
public static String getString(String jsonData, String key, String defaultValue) {
    if (StringUtils.isEmpty(jsonData)) {
        return defaultValue;
    }

    try {
        JSONObject jsonObject = new JSONObject(jsonData);
        return getString(jsonObject, key, defaultValue);
    } catch (JSONException e) {
        if (isPrintException) {
            e.printStackTrace();
        }
        return defaultValue;
    }
}

From source file:com.extremeboredom.wordattack.utils.JSONUtils.java

/**
 * get String array from jsonData//  w w  w  .  j a va 2s. c om
 *
 * @param jsonData
 * @param key
 * @param defaultValue
 * @return <ul>
 * <li>if jsonObject is null, return defaultValue</li>
 * <li>if jsonData {@link JSONObject#JSONObject(String)} exception, return defaultValue</li>
 * <li>return {@link JSONUtils#getStringArray(JSONObject, String, String[])}</li>
 * </ul>
 */
public static String[] getStringArray(String jsonData, String key, String[] defaultValue) {
    if (StringUtils.isEmpty(jsonData)) {
        return defaultValue;
    }

    try {
        JSONObject jsonObject = new JSONObject(jsonData);
        return getStringArray(jsonObject, key, defaultValue);
    } catch (JSONException e) {
        if (isPrintException) {
            e.printStackTrace();
        }
        return defaultValue;
    }
}

From source file:com.extremeboredom.wordattack.utils.JSONUtils.java

/**
 * get String list from jsonData// w  w  w.  ja v  a  2 s .  c o  m
 *
 * @param jsonData
 * @param key
 * @param defaultValue
 * @return <ul>
 * <li>if jsonObject is null, return defaultValue</li>
 * <li>if jsonData {@link JSONObject#JSONObject(String)} exception, return defaultValue</li>
 * <li>return {@link JSONUtils#getStringList(JSONObject, String, List)}</li>
 * </ul>
 */
public static List<String> getStringList(String jsonData, String key, List<String> defaultValue) {
    if (StringUtils.isEmpty(jsonData)) {
        return defaultValue;
    }

    try {
        JSONObject jsonObject = new JSONObject(jsonData);
        return getStringList(jsonObject, key, defaultValue);
    } catch (JSONException e) {
        if (isPrintException) {
            e.printStackTrace();
        }
        return defaultValue;
    }
}

From source file:com.extremeboredom.wordattack.utils.JSONUtils.java

/**
 * get JSONObject from jsonData//from   www . j  a  va 2s  .  co m
 *
 * @param jsonData
 * @param key
 * @param defaultValue
 * @return <ul>
 * <li>if jsonData is null, return defaultValue</li>
 * <li>if jsonData {@link JSONObject#JSONObject(String)} exception, return defaultValue</li>
 * <li>return {@link JSONUtils#getJSONObject(JSONObject, String, JSONObject)}</li>
 * </ul>
 */
public static JSONObject getJSONObject(String jsonData, String key, JSONObject defaultValue) {
    if (StringUtils.isEmpty(jsonData)) {
        return defaultValue;
    }

    try {
        JSONObject jsonObject = new JSONObject(jsonData);
        return getJSONObject(jsonObject, key, defaultValue);
    } catch (JSONException e) {
        if (isPrintException) {
            e.printStackTrace();
        }
        return defaultValue;
    }
}