|
| 1 | +/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON) |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License.*/ |
| 14 | + |
| 15 | +package unitauto; |
| 16 | + |
| 17 | +import com.alibaba.fastjson.JSONArray; |
| 18 | +import com.alibaba.fastjson.JSONObject; |
| 19 | +import com.alibaba.fastjson.parser.Feature; |
| 20 | +import com.alibaba.fastjson.serializer.SerializerFeature; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | + |
| 25 | +/**阿里FastJSON封装类 防止解析时异常 |
| 26 | + * @author Lemon |
| 27 | + */ |
| 28 | +public class JSON { |
| 29 | + private static final String TAG = "JSON"; |
| 30 | + |
| 31 | + /**判断json格式是否正确 |
| 32 | + * @param s |
| 33 | + * @return |
| 34 | + */ |
| 35 | + public static boolean isJsonCorrect(String s) { |
| 36 | + //太长 Log.i(TAG, "isJsonCorrect <<<< " + s + " >>>>>>>"); |
| 37 | + if (s == null |
| 38 | + // || s.equals("[]") |
| 39 | + // || s.equals("{}") |
| 40 | + || s.equals("") |
| 41 | + || s.equals("[null]") |
| 42 | + || s.equals("{null}") |
| 43 | + || s.equals("null")) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + /**获取有效的json |
| 50 | + * @param s |
| 51 | + * @return |
| 52 | + */ |
| 53 | + public static String getCorrectJson(String s) { |
| 54 | + return getCorrectJson(s, false); |
| 55 | + } |
| 56 | + /**获取有效的json |
| 57 | + * @param s |
| 58 | + * @param isArray |
| 59 | + * @return |
| 60 | + */ |
| 61 | + public static String getCorrectJson(String s, boolean isArray) { |
| 62 | + s = StringUtil.getTrimedString(s); |
| 63 | + // if (isArray) { |
| 64 | + // while (s.startsWith("\"")) { |
| 65 | + // s = s.substring(1); |
| 66 | + // } |
| 67 | + // while (s.endsWith("\"")) { |
| 68 | + // s = s.substring(0, s.length() - 1); |
| 69 | + // } |
| 70 | + // } |
| 71 | + return s;//isJsonCorrect(s) ? s : null; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param obj |
| 76 | + * @return |
| 77 | + */ |
| 78 | + public static Object parse(Object obj) { |
| 79 | + int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE; |
| 80 | + features |= Feature.OrderedField.getMask(); |
| 81 | + try { |
| 82 | + return com.alibaba.fastjson.JSON.parse(obj instanceof String ? (String) obj : toJSONString(obj), features); |
| 83 | + } catch (Exception e) { |
| 84 | + Log.i(TAG, "parse catch \n" + e.getMessage()); |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
| 88 | + /**obj转JSONObject |
| 89 | + * @param obj |
| 90 | + * @return |
| 91 | + */ |
| 92 | + public static JSONObject parseObject(Object obj) { |
| 93 | + if (obj instanceof JSONObject) { |
| 94 | + return (JSONObject) obj; |
| 95 | + } |
| 96 | + return parseObject(toJSONString(obj)); |
| 97 | + } |
| 98 | + /**json转JSONObject |
| 99 | + * @param json |
| 100 | + * @return |
| 101 | + */ |
| 102 | + public static JSONObject parseObject(String json) { |
| 103 | + int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE; |
| 104 | + features |= Feature.OrderedField.getMask(); |
| 105 | + return parseObject(json, features); |
| 106 | + } |
| 107 | + /**json转JSONObject |
| 108 | + * @param json |
| 109 | + * @param features |
| 110 | + * @return |
| 111 | + */ |
| 112 | + public static JSONObject parseObject(String json, int features) { |
| 113 | + try { |
| 114 | + return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), JSONObject.class, features); |
| 115 | + } catch (Exception e) { |
| 116 | + Log.i(TAG, "parseObject catch \n" + e.getMessage()); |
| 117 | + } |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + /**JSONObject转实体类 |
| 122 | + * @param object |
| 123 | + * @param clazz |
| 124 | + * @return |
| 125 | + */ |
| 126 | + public static <T> T parseObject(JSONObject object, Class<T> clazz) { |
| 127 | + return parseObject(toJSONString(object), clazz); |
| 128 | + } |
| 129 | + /**json转实体类 |
| 130 | + * @param json |
| 131 | + * @param clazz |
| 132 | + * @return |
| 133 | + */ |
| 134 | + public static <T> T parseObject(String json, Class<T> clazz) { |
| 135 | + if (clazz == null) { |
| 136 | + Log.e(TAG, "parseObject clazz == null >> return null;"); |
| 137 | + } else { |
| 138 | + try { |
| 139 | + int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE; |
| 140 | + features |= Feature.OrderedField.getMask(); |
| 141 | + return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features); |
| 142 | + } catch (Exception e) { |
| 143 | + Log.i(TAG, "parseObject catch \n" + e.getMessage()); |
| 144 | + } |
| 145 | + } |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + /**list转JSONArray |
| 150 | + * @param list |
| 151 | + * @return |
| 152 | + */ |
| 153 | + public static JSONArray parseArray(List<Object> list) { |
| 154 | + return new JSONArray(list); |
| 155 | + } |
| 156 | + /**obj转JSONArray |
| 157 | + * @param obj |
| 158 | + * @return |
| 159 | + */ |
| 160 | + public static JSONArray parseArray(Object obj) { |
| 161 | + if (obj instanceof JSONArray) { |
| 162 | + return (JSONArray) obj; |
| 163 | + } |
| 164 | + return parseArray(toJSONString(obj)); |
| 165 | + } |
| 166 | + /**json转JSONArray |
| 167 | + * @param json |
| 168 | + * @return |
| 169 | + */ |
| 170 | + public static JSONArray parseArray(String json) { |
| 171 | + try { |
| 172 | + return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true)); |
| 173 | + } catch (Exception e) { |
| 174 | + Log.i(TAG, "parseArray catch \n" + e.getMessage()); |
| 175 | + } |
| 176 | + return null; |
| 177 | + } |
| 178 | + /**JSONArray转实体类列表 |
| 179 | + * @param array |
| 180 | + * @param clazz |
| 181 | + * @return |
| 182 | + */ |
| 183 | + public static <T> List<T> parseArray(JSONArray array, Class<T> clazz) { |
| 184 | + return parseArray(toJSONString(array), clazz); |
| 185 | + } |
| 186 | + /**json转实体类列表 |
| 187 | + * @param json |
| 188 | + * @param clazz |
| 189 | + * @return |
| 190 | + */ |
| 191 | + public static <T> List<T> parseArray(String json, Class<T> clazz) { |
| 192 | + if (clazz == null) { |
| 193 | + Log.e(TAG, "parseArray clazz == null >> return null;"); |
| 194 | + } else { |
| 195 | + try { |
| 196 | + return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json, true), clazz); |
| 197 | + } catch (Exception e) { |
| 198 | + Log.i(TAG, "parseArray catch \n" + e.getMessage()); |
| 199 | + } |
| 200 | + } |
| 201 | + return null; |
| 202 | + } |
| 203 | + |
| 204 | + /**实体类转json |
| 205 | + * @param obj |
| 206 | + * @return |
| 207 | + */ |
| 208 | + public static String toJSONString(Object obj) { |
| 209 | + if (obj instanceof String) { |
| 210 | + return (String) obj; |
| 211 | + } |
| 212 | + try { |
| 213 | + return com.alibaba.fastjson.JSON.toJSONString(obj); |
| 214 | + } catch (Exception e) { |
| 215 | + Log.e(TAG, "toJSONString catch \n" + e.getMessage()); |
| 216 | + } |
| 217 | + return null; |
| 218 | + } |
| 219 | + |
| 220 | + /**实体类转json |
| 221 | + * @param obj |
| 222 | + * @param features |
| 223 | + * @return |
| 224 | + */ |
| 225 | + public static String toJSONString(Object obj, SerializerFeature... features) { |
| 226 | + if (obj instanceof String) { |
| 227 | + return (String) obj; |
| 228 | + } |
| 229 | + try { |
| 230 | + return com.alibaba.fastjson.JSON.toJSONString(obj, features); |
| 231 | + } catch (Exception e) { |
| 232 | + Log.e(TAG, "parseArray catch \n" + e.getMessage()); |
| 233 | + } |
| 234 | + return null; |
| 235 | + } |
| 236 | + |
| 237 | + /**格式化,显示更好看 |
| 238 | + * @param json |
| 239 | + * @return |
| 240 | + */ |
| 241 | + public static String format(String json) { |
| 242 | + return format(parse(json)); |
| 243 | + } |
| 244 | + /**格式化,显示更好看 |
| 245 | + * @param object |
| 246 | + * @return |
| 247 | + */ |
| 248 | + public static String format(Object object) { |
| 249 | + return toJSONString(object, SerializerFeature.PrettyFormat); |
| 250 | + } |
| 251 | + |
| 252 | + /**判断是否为JSONObject |
| 253 | + * @param obj instanceof String ? parseObject |
| 254 | + * @return |
| 255 | + */ |
| 256 | + public static boolean isJSONObject(Object obj) { |
| 257 | + if (obj instanceof JSONObject) { |
| 258 | + return true; |
| 259 | + } |
| 260 | + if (obj instanceof String) { |
| 261 | + try { |
| 262 | + JSONObject json = parseObject((String) obj); |
| 263 | + return json != null && json.isEmpty() == false; |
| 264 | + } catch (Exception e) { |
| 265 | + Log.e(TAG, "isJSONObject catch \n" + e.getMessage()); |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + return false; |
| 270 | + } |
| 271 | + /**判断是否为JSONArray |
| 272 | + * @param obj instanceof String ? parseArray |
| 273 | + * @return |
| 274 | + */ |
| 275 | + public static boolean isJSONArray(Object obj) { |
| 276 | + if (obj instanceof JSONArray) { |
| 277 | + return true; |
| 278 | + } |
| 279 | + if (obj instanceof String) { |
| 280 | + try { |
| 281 | + JSONArray json = parseArray((String) obj); |
| 282 | + return json != null && json.isEmpty() == false; |
| 283 | + } catch (Exception e) { |
| 284 | + Log.e(TAG, "isJSONArray catch \n" + e.getMessage()); |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + return false; |
| 289 | + } |
| 290 | + |
| 291 | + /**判断是否为 Boolean,Number,String 中的一种 |
| 292 | + * @param obj |
| 293 | + * @return |
| 294 | + */ |
| 295 | + public static boolean isBooleanOrNumberOrString(Object obj) { |
| 296 | + return obj instanceof Boolean || obj instanceof Number || obj instanceof String; |
| 297 | + } |
| 298 | + |
| 299 | +} |
0 commit comments