|
| 1 | +package com.gyf.toolutils4a; |
| 2 | + |
| 3 | +import org.json.JSONArray; |
| 4 | +import org.json.JSONException; |
| 5 | +import org.json.JSONObject; |
| 6 | +import org.json.JSONTokener; |
| 7 | + |
| 8 | +import java.lang.reflect.Array; |
| 9 | +import java.util.Collection; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +/** |
| 13 | + * Msg: JSON处理类 1、Map转为JSONObject 2、集合转换为JSONArray |
| 14 | + * Created by gyf on 2016/11/15. |
| 15 | + */ |
| 16 | + |
| 17 | +public class JsonUtils { |
| 18 | + /** |
| 19 | + * Map转为JSON对象 |
| 20 | + * |
| 21 | + * @param data Map数据 |
| 22 | + * @return |
| 23 | + */ |
| 24 | + public static JSONObject mapToJson(Map<?, ?> data) { |
| 25 | + JSONObject object = new JSONObject(); |
| 26 | + |
| 27 | + for (Map.Entry<?, ?> entry : data.entrySet()) { |
| 28 | + String key = (String) entry.getKey(); |
| 29 | + if (key == null) { |
| 30 | + throw new NullPointerException("key == null"); |
| 31 | + } |
| 32 | + try { |
| 33 | + object.put(key, wrap(entry.getValue())); |
| 34 | + } catch (JSONException e) { |
| 35 | + e.printStackTrace(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return object; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * 集合转换为JSONArray |
| 44 | + * |
| 45 | + * @param data 集合对象 |
| 46 | + * @return |
| 47 | + */ |
| 48 | + public static JSONArray collectionToJson(Collection<?> data) { |
| 49 | + JSONArray jsonArray = new JSONArray(); |
| 50 | + if (data != null) { |
| 51 | + for (Object aData : data) { |
| 52 | + jsonArray.put(wrap(aData)); |
| 53 | + } |
| 54 | + } |
| 55 | + return jsonArray; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Object对象转换为JSONArray |
| 60 | + * |
| 61 | + * @param data Object对象 |
| 62 | + * @return |
| 63 | + * @throws JSONException Object非数组时,抛出异常 |
| 64 | + */ |
| 65 | + public static JSONArray arrayToJson(Object data) throws JSONException { |
| 66 | + if (!data.getClass().isArray()) { |
| 67 | + throw new JSONException("Not a primitive data: " + data.getClass()); |
| 68 | + } |
| 69 | + final int length = Array.getLength(data); |
| 70 | + JSONArray jsonArray = new JSONArray(); |
| 71 | + for (int i = 0; i < length; ++i) { |
| 72 | + jsonArray.put(wrap(Array.get(data, i))); |
| 73 | + } |
| 74 | + |
| 75 | + return jsonArray; |
| 76 | + } |
| 77 | + |
| 78 | + private static Object wrap(Object o) { |
| 79 | + if (o == null) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + if (o instanceof JSONArray || o instanceof JSONObject) { |
| 83 | + return o; |
| 84 | + } |
| 85 | + try { |
| 86 | + if (o instanceof Collection) { |
| 87 | + return collectionToJson((Collection<?>) o); |
| 88 | + } else if (o.getClass().isArray()) { |
| 89 | + return arrayToJson(o); |
| 90 | + } |
| 91 | + if (o instanceof Map) { |
| 92 | + return mapToJson((Map<?, ?>) o); |
| 93 | + } |
| 94 | + |
| 95 | + if (o instanceof Boolean || o instanceof Byte || o instanceof Character || o instanceof Double || o instanceof Float || o instanceof Integer || o instanceof Long |
| 96 | + || o instanceof Short || o instanceof String) { |
| 97 | + return o; |
| 98 | + } |
| 99 | + if (o.getClass().getPackage().getName().startsWith("java.")) { |
| 100 | + return o.toString(); |
| 101 | + } |
| 102 | + } catch (Exception ignored) { |
| 103 | + } |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * 根据json字符串生成JSONObject对象 |
| 109 | + * |
| 110 | + * @param json json字符串 |
| 111 | + * @return 返回JSONObject对象或者空(null) |
| 112 | + */ |
| 113 | + public static JSONObject initJSONObject(String json) { |
| 114 | + |
| 115 | + JSONObject jsonObject = null; |
| 116 | + try { |
| 117 | + JSONTokener jsonParser = new JSONTokener(json); |
| 118 | + jsonObject = (JSONObject) jsonParser.nextValue(); |
| 119 | + } catch (Exception e) { |
| 120 | + } finally { |
| 121 | + return jsonObject; |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * 根据Key值相对应的Value值 |
| 128 | + * |
| 129 | + * @param jsonObject Json对象 |
| 130 | + * @param key 健值 |
| 131 | + * @return 返回value值或空串("") |
| 132 | + */ |
| 133 | + public static String getString(JSONObject jsonObject, String key) { |
| 134 | + String value = ""; |
| 135 | + try { |
| 136 | + value = jsonObject.getString(key); |
| 137 | + } catch (Exception e) { |
| 138 | + } |
| 139 | + |
| 140 | + return value; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * 根据Key值相对应的Value值 |
| 145 | + * |
| 146 | + * @param jsonObject Json对象 |
| 147 | + * @param key 健值 |
| 148 | + * @return 返回value值或-1 |
| 149 | + */ |
| 150 | + public static int getInt(JSONObject jsonObject, String key) { |
| 151 | + int value = -1; |
| 152 | + try { |
| 153 | + value = jsonObject.getInt(key); |
| 154 | + } catch (Exception e) { |
| 155 | + } |
| 156 | + |
| 157 | + return value; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * 根据Key值相对应的Value值 |
| 162 | + * |
| 163 | + * @param jsonObject Json对象 |
| 164 | + * @param key 健值 |
| 165 | + * @return 返回value值或-1 |
| 166 | + */ |
| 167 | + public static long getLong(JSONObject jsonObject, String key) { |
| 168 | + long value = -1; |
| 169 | + try { |
| 170 | + value = jsonObject.getLong(key); |
| 171 | + } catch (Exception e) { |
| 172 | + } |
| 173 | + |
| 174 | + return value; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * 根据Key值相对应的Value值 |
| 179 | + * |
| 180 | + * @param jsonObject Json对象 |
| 181 | + * @param key 健值 |
| 182 | + * @return 返回value值或null |
| 183 | + */ |
| 184 | + public static JSONArray getJSONArray(JSONObject jsonObject, String key) { |
| 185 | + JSONArray arrays = null; |
| 186 | + try { |
| 187 | + arrays = jsonObject.getJSONArray(key); |
| 188 | + } catch (Exception e) { |
| 189 | + } |
| 190 | + |
| 191 | + return arrays; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * 根据Key值相对应的Value值 |
| 196 | + * |
| 197 | + * @param jsonArray JsonArray对象 |
| 198 | + * @param index 索引 |
| 199 | + * @return 返回value值或null |
| 200 | + */ |
| 201 | + public static JSONObject getJSONObject(JSONArray jsonArray, int index) { |
| 202 | + JSONObject jsonObject = null; |
| 203 | + try { |
| 204 | + jsonObject = jsonArray.getJSONObject(index); |
| 205 | + } catch (Exception e) { |
| 206 | + } |
| 207 | + return jsonObject; |
| 208 | + } |
| 209 | +} |
0 commit comments