|
1 |
| -//package com.blankj.utilcode.util; |
2 |
| -// |
3 |
| -// |
4 |
| -//import org.json.JSONException; |
5 |
| -//import org.json.JSONObject; |
6 |
| -// |
7 |
| -//public final class JsonUtils { |
8 |
| -// |
9 |
| -// private static byte TYPE_LONG = 0x01; |
10 |
| -// private static byte TYPE_INT = 0x02; |
11 |
| -// |
12 |
| -// private JsonUtils() { |
13 |
| -// throw new UnsupportedOperationException("u can't instantiate me..."); |
14 |
| -// } |
15 |
| -// |
16 |
| -// public static long getLong(final JSONObject jsonObject, |
17 |
| -// final String key, |
18 |
| -// final long defaultValue) { |
19 |
| -// if (jsonObject == null || key == null || key.length() == 0) { |
20 |
| -// return defaultValue; |
21 |
| -// } |
22 |
| -// try { |
23 |
| -// return jsonObject.getLong(key); |
24 |
| -// } catch (JSONException e) { |
25 |
| -// return defaultValue; |
26 |
| -// } |
27 |
| -// } |
28 |
| -// |
29 |
| -// public static long getLong(final String json, |
30 |
| -// final String key, |
31 |
| -// final long defaultValue) { |
32 |
| -// if (json == null || json.length() == 0 |
33 |
| -// || key == null || key.length() == 0) { |
34 |
| -// return defaultValue; |
35 |
| -// } |
36 |
| -// |
37 |
| -// try { |
38 |
| -// return getLong(new JSONObject(json), key, defaultValue); |
39 |
| -// } catch (JSONException e) { |
40 |
| -// e.printStackTrace(); |
41 |
| -// return defaultValue; |
42 |
| -// } |
43 |
| -// } |
44 |
| -// |
45 |
| -// public static Object getType(final JSONObject jsonObject, |
46 |
| -// final String key, |
47 |
| -// final Object defaultValue, |
48 |
| -// final byte type) { |
49 |
| -// if (jsonObject == null || key == null || key.length() == 0) { |
50 |
| -// return defaultValue; |
51 |
| -// } |
52 |
| -// try { |
53 |
| -// if (type == TYPE_LONG) { |
54 |
| -// return jsonObject.getLong(key); |
55 |
| -// } else if (type == TYPE_INT) { |
56 |
| -// return jsonObject.getInt(key); |
57 |
| -// } |
58 |
| -// } catch (JSONException e) { |
59 |
| -// return defaultValue; |
60 |
| -// } |
61 |
| -// } |
62 |
| -// |
63 |
| -// public static long getLong(final String json, |
64 |
| -// final String key, |
65 |
| -// final long defaultValue) { |
66 |
| -// if (json == null || json.length() == 0 |
67 |
| -// || key == null || key.length() == 0) { |
68 |
| -// return defaultValue; |
69 |
| -// } |
70 |
| -// |
71 |
| -// try { |
72 |
| -// return getLong(new JSONObject(json), key, defaultValue); |
73 |
| -// } catch (JSONException e) { |
74 |
| -// e.printStackTrace(); |
75 |
| -// return defaultValue; |
76 |
| -// } |
77 |
| -// } |
78 |
| -// |
79 |
| -//} |
| 1 | +package com.blankj.utilcode.util; |
| 2 | + |
| 3 | + |
| 4 | +import android.util.Log; |
| 5 | + |
| 6 | +import org.json.JSONArray; |
| 7 | +import org.json.JSONException; |
| 8 | +import org.json.JSONObject; |
| 9 | + |
| 10 | +public final class JsonUtils { |
| 11 | + |
| 12 | + private static final byte TYPE_BOOLEAN = 0x00; |
| 13 | + private static final byte TYPE_INT = 0x01; |
| 14 | + private static final byte TYPE_LONG = 0x02; |
| 15 | + private static final byte TYPE_DOUBLE = 0x03; |
| 16 | + private static final byte TYPE_STRING = 0x04; |
| 17 | + private static final byte TYPE_JSON_OBJECT = 0x05; |
| 18 | + private static final byte TYPE_JSON_ARRAY = 0x06; |
| 19 | + |
| 20 | + private JsonUtils() { |
| 21 | + throw new UnsupportedOperationException("u can't instantiate me..."); |
| 22 | + } |
| 23 | + |
| 24 | + public static boolean getBoolean(final JSONObject jsonObject, |
| 25 | + final String key) { |
| 26 | + return getBoolean(jsonObject, key, false); |
| 27 | + } |
| 28 | + |
| 29 | + public static boolean getBoolean(final JSONObject jsonObject, |
| 30 | + final String key, |
| 31 | + final boolean defaultValue) { |
| 32 | + return getValueByType(jsonObject, key, defaultValue, TYPE_BOOLEAN); |
| 33 | + } |
| 34 | + |
| 35 | + public static boolean getBoolean(final String json, |
| 36 | + final String key) { |
| 37 | + return getBoolean(json, key, false); |
| 38 | + } |
| 39 | + |
| 40 | + public static boolean getBoolean(final String json, |
| 41 | + final String key, |
| 42 | + final boolean defaultValue) { |
| 43 | + return getValueByType(json, key, defaultValue, TYPE_BOOLEAN); |
| 44 | + } |
| 45 | + |
| 46 | + public static int getInt(final JSONObject jsonObject, |
| 47 | + final String key) { |
| 48 | + return getInt(jsonObject, key, -1); |
| 49 | + } |
| 50 | + |
| 51 | + public static int getInt(final JSONObject jsonObject, |
| 52 | + final String key, |
| 53 | + final int defaultValue) { |
| 54 | + return getValueByType(jsonObject, key, defaultValue, TYPE_INT); |
| 55 | + } |
| 56 | + |
| 57 | + public static int getInt(final String json, |
| 58 | + final String key) { |
| 59 | + return getInt(json, key, -1); |
| 60 | + } |
| 61 | + |
| 62 | + public static int getInt(final String json, |
| 63 | + final String key, |
| 64 | + final int defaultValue) { |
| 65 | + return getValueByType(json, key, defaultValue, TYPE_INT); |
| 66 | + } |
| 67 | + |
| 68 | + public static long getLong(final JSONObject jsonObject, |
| 69 | + final String key) { |
| 70 | + return getLong(jsonObject, key, -1); |
| 71 | + } |
| 72 | + |
| 73 | + public static long getLong(final JSONObject jsonObject, |
| 74 | + final String key, |
| 75 | + final long defaultValue) { |
| 76 | + return getValueByType(jsonObject, key, defaultValue, TYPE_LONG); |
| 77 | + } |
| 78 | + |
| 79 | + public static long getLong(final String json, |
| 80 | + final String key) { |
| 81 | + return getLong(json, key, -1); |
| 82 | + } |
| 83 | + |
| 84 | + public static long getLong(final String json, |
| 85 | + final String key, |
| 86 | + final long defaultValue) { |
| 87 | + return getValueByType(json, key, defaultValue, TYPE_LONG); |
| 88 | + } |
| 89 | + |
| 90 | + public static double getDouble(final JSONObject jsonObject, |
| 91 | + final String key) { |
| 92 | + return getDouble(jsonObject, key, -1); |
| 93 | + } |
| 94 | + |
| 95 | + public static double getDouble(final JSONObject jsonObject, |
| 96 | + final String key, |
| 97 | + final double defaultValue) { |
| 98 | + return getValueByType(jsonObject, key, defaultValue, TYPE_DOUBLE); |
| 99 | + } |
| 100 | + |
| 101 | + public static double getDouble(final String json, |
| 102 | + final String key) { |
| 103 | + return getDouble(json, key, -1); |
| 104 | + } |
| 105 | + |
| 106 | + public static double getDouble(final String json, |
| 107 | + final String key, |
| 108 | + final double defaultValue) { |
| 109 | + return getValueByType(json, key, defaultValue, TYPE_DOUBLE); |
| 110 | + } |
| 111 | + |
| 112 | + public static String getString(final JSONObject jsonObject, |
| 113 | + final String key) { |
| 114 | + return getString(jsonObject, key, ""); |
| 115 | + } |
| 116 | + |
| 117 | + public static String getString(final JSONObject jsonObject, |
| 118 | + final String key, |
| 119 | + final String defaultValue) { |
| 120 | + return getValueByType(jsonObject, key, defaultValue, TYPE_STRING); |
| 121 | + } |
| 122 | + |
| 123 | + public static String getString(final String json, |
| 124 | + final String key) { |
| 125 | + return getString(json, key, ""); |
| 126 | + } |
| 127 | + |
| 128 | + public static String getString(final String json, |
| 129 | + final String key, |
| 130 | + final String defaultValue) { |
| 131 | + return getValueByType(json, key, defaultValue, TYPE_STRING); |
| 132 | + } |
| 133 | + |
| 134 | + public static JSONObject getJSONObject(final JSONObject jsonObject, |
| 135 | + final String key, |
| 136 | + final JSONObject defaultValue) { |
| 137 | + return getValueByType(jsonObject, key, defaultValue, TYPE_JSON_OBJECT); |
| 138 | + } |
| 139 | + |
| 140 | + public static JSONObject getJSONObject(final String json, |
| 141 | + final String key, |
| 142 | + final JSONObject defaultValue) { |
| 143 | + return getValueByType(json, key, defaultValue, TYPE_JSON_OBJECT); |
| 144 | + } |
| 145 | + |
| 146 | + public static JSONArray getJSONArray(final JSONObject jsonObject, |
| 147 | + final String key, |
| 148 | + final JSONArray defaultValue) { |
| 149 | + return getValueByType(jsonObject, key, defaultValue, TYPE_JSON_ARRAY); |
| 150 | + } |
| 151 | + |
| 152 | + public static JSONArray getJSONArray(final String json, |
| 153 | + final String key, |
| 154 | + final JSONArray defaultValue) { |
| 155 | + return getValueByType(json, key, defaultValue, TYPE_JSON_ARRAY); |
| 156 | + } |
| 157 | + |
| 158 | + private static <T> T getValueByType(final JSONObject jsonObject, |
| 159 | + final String key, |
| 160 | + final T defaultValue, |
| 161 | + final byte type) { |
| 162 | + if (jsonObject == null || key == null || key.length() == 0) { |
| 163 | + return defaultValue; |
| 164 | + } |
| 165 | + try { |
| 166 | + Object ret; |
| 167 | + if (type == TYPE_BOOLEAN) { |
| 168 | + ret = jsonObject.getBoolean(key); |
| 169 | + } else if (type == TYPE_INT) { |
| 170 | + ret = jsonObject.getInt(key); |
| 171 | + } else if (type == TYPE_LONG) { |
| 172 | + ret = jsonObject.getLong(key); |
| 173 | + } else if (type == TYPE_DOUBLE) { |
| 174 | + ret = jsonObject.getDouble(key); |
| 175 | + } else if (type == TYPE_STRING) { |
| 176 | + ret = jsonObject.getString(key); |
| 177 | + } else if (type == TYPE_JSON_OBJECT) { |
| 178 | + ret = jsonObject.getJSONObject(key); |
| 179 | + } else if (type == TYPE_JSON_ARRAY) { |
| 180 | + ret = jsonObject.getJSONArray(key); |
| 181 | + } else { |
| 182 | + return defaultValue; |
| 183 | + } |
| 184 | + //noinspection unchecked |
| 185 | + return (T) ret; |
| 186 | + } catch (JSONException e) { |
| 187 | + Log.e("JsonUtils", "getValueByType: ", e); |
| 188 | + return defaultValue; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private static <T> T getValueByType(final String json, |
| 193 | + final String key, |
| 194 | + final T defaultValue, |
| 195 | + final byte type) { |
| 196 | + if (json == null || json.length() == 0 |
| 197 | + || key == null || key.length() == 0) { |
| 198 | + return defaultValue; |
| 199 | + } |
| 200 | + try { |
| 201 | + return getValueByType(new JSONObject(json), key, defaultValue, type); |
| 202 | + } catch (JSONException e) { |
| 203 | + Log.e("JsonUtils", "getValueByType: ", e); |
| 204 | + return defaultValue; |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments