Java tutorial
//package com.java2s; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.json.JSONException; import org.json.JSONObject; public class Main { public static boolean isPrintException = true; /** * parse key-value pairs to map. ignore empty key, if getValue exception, put empty value * * @param sourceObj key-value pairs json * @return <ul> * <li>if sourceObj is null, return null</li> * <li>else parse entry by {@link MapUtils#putMapNotEmptyKey(Map, String, String)} one by one</li> * </ul> */ @SuppressWarnings("rawtypes") public static Map<String, String> parseKeyAndValueToMap(JSONObject sourceObj) { if (sourceObj == null) { return null; } Map<String, String> keyAndValueMap = new HashMap<String, String>(); for (Iterator iter = sourceObj.keys(); iter.hasNext();) { String key = (String) iter.next(); putMapNotEmptyKey(keyAndValueMap, key, getString(sourceObj, key, "")); } return keyAndValueMap; } /** * parse key-value pairs to map. ignore empty key, if getValue exception, put empty value * * @param source key-value pairs json * @return <ul> * <li>if source is null or source's length is 0, return empty map</li> * <li>if source {@link JSONObject#JSONObject(String)} exception, return null</li> * <li>return {@link JSONUtils#parseKeyAndValueToMap(JSONObject)}</li> * </ul> */ public static Map<String, String> parseKeyAndValueToMap(String source) { if (isEmpty(source)) { return null; } try { JSONObject jsonObject = new JSONObject(source); return parseKeyAndValueToMap(jsonObject); } catch (JSONException e) { if (isPrintException) { e.printStackTrace(); } return null; } } private static boolean putMapNotEmptyKey(Map<String, String> map, String key, String value) { if (map == null || isEmpty(key)) { return false; } map.put(key, value); return true; } /** * get String from jsonObject * * @param jsonObject * @param key * @param defaultValue * @return <ul> * <li>if jsonObject is null, return defaultValue</li> * <li>if key is null or empty, return defaultValue</li> * <li>if {@link JSONObject#getString(String)} exception, return defaultValue</li> * <li>return {@link JSONObject#getString(String)}</li> * </ul> */ public static String getString(JSONObject jsonObject, String key, String defaultValue) { if (jsonObject == null || isEmpty(key)) { return defaultValue; } try { return jsonObject.getString(key); } catch (JSONException e) { if (isPrintException) { e.printStackTrace(); } return defaultValue; } } /** * get String from jsonData * * @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, JSONObject)}</li> * </ul> */ public static String getString(String jsonData, String key, String defaultValue) { if (isEmpty(jsonData)) { return defaultValue; } try { JSONObject jsonObject = new JSONObject(jsonData); return getString(jsonObject, key, defaultValue); } catch (JSONException e) { if (isPrintException) { e.printStackTrace(); } return defaultValue; } } private static boolean isEmpty(CharSequence str) { return (str == null || str.length() == 0); } private static <V> boolean isEmpty(V[] sourceArray) { return (sourceArray == null || sourceArray.length == 0); } }