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));
  }

你可能感兴趣的:(java8,java)