java實現-JSONArray按照時間排序

需求

已知業務方傳過來的json是中包含一個JSONArray,我需要用ognl表達式將JSONArry轉成List《Object》而且還需要根據時間排序(由於業務方查詢了多個表未能按時間排序)

假設一個json如下

String s =
          "{\n"
              + "    \"da_a_getUserCreditInfoByIdCard\": {\n"
              + "        \"control\": {\n"
              + "            \"serverTime\": 1586486461326,\n"
              + "            \"error\": 0,\n"
              + "            \"message\": \"操作成功\"\n"
              + "        },\n"
              + "        \"data\": [\n"
              + "            {\n"
              + "                \"user_id\": 60000819,\n"
              + "                \"source\": \"weibo\",\n"
              + "                \"credit_id\": 40605092228,\n"
              + "                \"user_name\": \"唐源密\",\n"
              + "                \"apply_time\": \"2020-04-01 10:50:36\",\n"
              + "                \"audit_time\": \"2020-04-01 10:50:36\",\n"
              + "                \"apply_status\": \"001002006\",\n"
              + "                \"credit_amount\": 300000,\n"
              + "                \"apply_type\": \"001006001\",\n"
              + "                \"reject_day\": 2,\n"
              + "                \"auth_code\": \"basic\",\n"
              + "                \"type_code\": \"A3\",\n"
              + "                \"group_code\": \"xiaoe\",\n"
              + "                \"item_code\": \"base\",\n"
              + "                \"user_type\": \"b012\",\n"
              + "                \"sku\": \"cycle_loan\",\n"
              + "                \"sub_source\": \"000007001\",\n"
              + "                \"brand_name\": \"weibo_jieqian\",\n"
              + "                \"channel\": \"meitu\",\n"
              + "                \"create_time\": \"2020-04-01 10:50:36\",\n"
              + "                \"ctime\": \"2020-04-01 10:50:36\"\n"
              + "            },\n"
              + "            {\n"
              + "                \"user_id\": 60000819,\n"
              + "                \"source\": \"weibo\",\n"
              + "                \"credit_id\": 40605092228,\n"
              + "                \"user_name\": \"唐源密\",\n"
              + "                \"apply_time\": \"2020-04-01 10:50:36\",\n"
              + "                \"audit_time\": \"2020-04-01 10:50:36\",\n"
              + "                \"apply_status\": \"001002005\",\n"
              + "                \"credit_amount\": 300000,\n"
              + "                \"apply_type\": \"001006001\",\n"
              + "                \"reject_day\": 1,\n"
              + "                \"auth_code\": \"basic\",\n"
              + "                \"type_code\": \"A3\",\n"
              + "                \"group_code\": \"xiaoe\",\n"
              + "                \"item_code\": \"base\",\n"
              + "                \"user_type\": \"b012\",\n"
              + "                \"sku\": \"cycle_loan\",\n"
              + "                \"sub_source\": \"000007001\",\n"
              + "                \"brand_name\": \"weibo_jieqian\",\n"
              + "                \"channel\": \"wifiyaoshi\",\n"
              + "                \"create_time\": \"2020-04-01 10:50:36\",\n"
              + "                \"ctime\": \"2020-04-01 10:50:36\"\n"
              + "            }\n"
              + "        ]\n"
              + "    }\n"
              + "}";

ognl獲取list

String ognl =
          "#ret=da_a_getUserCreditInfoByIdCard.data.{? #this.channel!=\"weibo\" && sku==\"cycle_loan\"},#[email protected]@getLateastApplyStatys(#ret),#param=#rets.get(0).apply_status";

ognl中調用的方法實現如下:

public static List<Map<String, Object>> getLateastApplyStatys(List<Object> objList) {
    String json = JSONObject.toJSONString(objList);
    List<Map<String, Object>> rewardModelList = (List<Map<String, Object>>) JSON.parse(json);
    rewardModelList.sort((a,b) -> {
      String s = a.get("create_time").toString();
      String s1 = b.get("create_time").toString();
      try {
        return DateUtil.formatToDayByYYYYMMDDMMHHSS(s).getTime() - DateUtil.formatToDayByYYYYMMDDMMHHSS(s1).getTime() >= 0 ? -1 : 1;
      } catch (ParseException e) {
        e.printStackTrace();
      }
      return 0;
    });
    return rewardModelList;
  }

  public static void main(String[] args) {
    String json=
            "[{\"item_code\":\"base\",\"sub_source\":\"000007001\",\"group_code\":\"xiaoe\",\"create_time\":\"2020-04-02 10:50:36\",\"user_name\":\"唐源密\",\"credit_amount\":300000,\"channel\":\"meitu\",\"apply_status\":\"001002006\",\"apply_time\":\"2020-04-01 10:50:36\",\"brand_name\":\"weibo_jieqian\",\"source\":\"weibo\",\"auth_code\":\"basic\",\"audit_time\":\"2020-04-01 10:50:36\",\"user_type\":\"b012\",\"user_id\":60000819,\"credit_id\":40605092228,\"reject_day\":2,\"ctime\":\"2020-04-01 10:50:36\",\"sku\":\"cycle_loan\",\"apply_type\":\"001006001\",\"type_code\":\"A3\"}, {\"item_code\":\"base\",\"sub_source\":\"000007001\",\"group_code\":\"xiaoe\",\"create_time\":\"2020-04-01 10:50:36\",\"user_name\":\"唐源密\",\"credit_amount\":300000,\"channel\":\"wifiyaoshi\",\"apply_status\":\"001002005\",\"apply_time\":\"2020-04-01 10:50:36\",\"brand_name\":\"weibo_jieqian\",\"source\":\"weibo\",\"auth_code\":\"basic\",\"audit_time\":\"2020-04-01 10:50:36\",\"user_type\":\"b012\",\"user_id\":60000819,\"credit_id\":40605092228,\"reject_day\":1,\"ctime\":\"2020-04-01 10:50:36\",\"sku\":\"cycle_loan\",\"apply_type\":\"001006001\",\"type_code\":\"A3\"}]";
    List<Object> objects = JSONObject.parseArray(json, Object.class);
    List<Map<String, Object>> lateastApplyStatys = getLateastApplyStatys(objects);
    System.out.println(JSONObject.toJSONString(lateastApplyStatys));
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章