package com.shopcider.plutus.component.util; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.shopcider.plutus.component.exception.BizException; import java.util.Map; public class JSONUtil { /** * 返回的一定是null * @param exception * @param errorMessage * @return */ public static <T> T nullOrException(boolean exception, String errorMessage) { if(exception) { throw new BizException(errorMessage); } else { return null; } } public static JSONObject getJSONObjectHasArrayByKeys(JSONObject jsonObject, Map<String, Integer> arrayKeyAndIndex, String... keys) { return getJSONObjectHasArrayByKeys(jsonObject, arrayKeyAndIndex, true, keys); } /** * 连续找jsonObject避免空指针原因太难发现 * @param jsonObject 保证了返回的一定不为null * @param keys * @return */ public static JSONObject getJSONObjectByKeys(JSONObject jsonObject, String... keys) { return getJSONObjectByKeys(jsonObject, true, keys); } public static JSONArray getJSONArrayByKeys(JSONObject jsonObject, String... keys) { return getJSONArrayByKeys(jsonObject, true, keys); } /** * 连续找jsonObject避免空指针原因太难发现 * @param jsonObject 保证了返回的一定不为null * @param arrayKeyAndIndex 支持负数从倒数开始,但是绝对值小于size * @param keys * @return */ public static JSONObject getJSONObjectHasArrayByKeys(JSONObject jsonObject, Map<String, Integer> arrayKeyAndIndex, boolean exception, String... keys) { if(jsonObject == null || arrayKeyAndIndex == null) { return nullOrException(exception,"jsonObject and keyAndIndex must not null!"); } StringBuilder sb = new StringBuilder(); JSONObject t = jsonObject; Object o; for(String k : keys) { Integer i = arrayKeyAndIndex.get(k); o = t.get(k); sb.append(k); if(o == null) { return nullOrException(exception, "找不到对应的key: " + sb.toString() + " 对象: " + jsonObject.toJSONString()); } //json数组的处理 if(i != null) { if(o instanceof JSONArray == false) { return nullOrException(exception, "key: " + sb.toString() + " 不是JSONArray类型而是 " + o.getClass().getName() + " 类型. 对象: " + jsonObject.toJSONString()); } JSONArray a = (JSONArray) o; if(a.size() <= Math.abs(i)) { return nullOrException(exception, "key: " + sb.toString() + " 对应的JSONArray大小为 " + a.size() + " ,获取不到以 " + i + " 为下标的数据。" + "对象: " + jsonObject.toJSONString()); } o = a.get(i < 0 ? a.size() + i : i); } //json对象的处理 if(o instanceof JSONObject == false) { return nullOrException(exception, "key: " + sb.toString() + " 不是ObjectJSON类型而是 " + o.getClass().getName() + " 类型. 对象: " + jsonObject.toJSONString()); } t = (JSONObject) o; sb.append('.'); } return t; } public static JSONArray getJSONArrayByKeys(JSONObject jsonObject, boolean exception, String... keys) { int lastIndex = keys.length - 1; if(lastIndex < 0) { return null; } else if(lastIndex == 0) { return jsonObject == null ? null : jsonObject.getJSONArray(keys[lastIndex]); } else { String[] tkeys = new String[lastIndex]; for(int i = 0;i < lastIndex;i++) { tkeys[i] = keys[i]; } JSONObject t = getJSONObjectByKeys(jsonObject, exception, tkeys); return t == null ? null : t.getJSONArray(keys[lastIndex]); } } public static JSONObject getJSONObjectByKeys(JSONObject jsonObject, boolean exception, String... keys) { if(jsonObject == null) { return nullOrException(exception, "jsonObject must not null!"); } JSONObject t = jsonObject; Object o; StringBuilder sb = new StringBuilder(); for(String k : keys) { o = t.get(k); sb.append(k); if(o == null) { return nullOrException(exception, "找不到对应的key: " + sb.toString() + " 对象: " + jsonObject.toJSONString()); } else if(o instanceof JSONObject == false) { return nullOrException(exception, "key: " + sb.toString() + " 不是ObjectJSON类型而是 " + o.getClass().getName() + " 类型. 对象: " + jsonObject.toJSONString()); } t = (JSONObject) o; sb.append('.'); } return t; } }