Java-有空值(NULL) JSONArray按字段排序(空值在最后)

本文介绍了一种使用Java对包含BigDecimal类型的JSON对象数组进行排序的方法,包括处理null值的技巧,展示了正序和倒序排列的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        JSONArray jsonArray = new JSONArray();

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("testField",4.12);
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("testField",4.18);
        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("testField",5.19);
        JSONObject jsonObject4 = new JSONObject();
        jsonObject4.put("testField",null);

        jsonArray.add(jsonObject1);
        jsonArray.add(jsonObject2);
        jsonArray.add(jsonObject3);
        jsonArray.add(jsonObject4);

        jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getBigDecimal("testField"),Comparator.nullsLast(BigDecimal::compareTo)));
        System.out.println("正序:");
        System.out.println(jsonArray);
        jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getBigDecimal("testField"),Comparator.nullsFirst(BigDecimal::compareTo)).reversed());
        System.out.println("倒序:");
        System.out.println(jsonArray);

public JSONObject validateRequest(String jsonStr) { JsonFactory factory = new JsonFactory(); try (JsonParser parser = factory.createParser(jsonStr)) { JsonToken token; JSONObject result = new JSONObject(); String currentField = null; while ((token = parser.nextToken()) != null) { switch (token) { case FIELD_NAME: currentField = parser.getCurrentName(); break; case VALUE_STRING: result.put(currentField, parser.getText()); break; case VALUE_NUMBER_INT: result.put(currentField, parser.getIntValue()); break; case VALUE_NULL: // 直接处理空值 result.put(currentField, ""); break; // 其他类型处理... } } return result; } catch (IOException e) { throw new RuntimeException(e); } } // 使用迭代替代递归,避免栈溢出 void iterativeReplaceNull(JSONObject json) { Deque<JSONObject> stack = new ArrayDeque<>(); stack.push(json); while (!stack.isEmpty()) { JSONObject current = stack.pop(); for (String key : current.keySet()) { Object value = current.get(key); if (value == null) { current.put(key, ""); // 替换空值 } else if (value instanceof JSONObject) { stack.push((JSONObject) value); } else if (value instanceof JSONArray) { processArray((JSONArray) value, stack); } } } } void processArray(JSONArray array, Deque<JSONObject> stack) { for (int i = 0; i < array.size(); i++) { Object item = array.get(i); if (item == null) { array.set(i, ""); } else if (item instanceof JSONObject) { stack.push((JSONObject) item); } else if (item instanceof JSONArray) { processArray((JSONArray) item, stack); } } } /** * 处理JSONObject对象 */ private void handleJSONObject(JSONObject json) { handleSpecialFields(json); for (String key : json.keySet()) { Object value = json.get(key); if (value == null || "null".equals(value)) { json.put(key, ""); } else if (value instanceof BigDecimal) { json.put(key, value.toString()); } else { iterativeReplaceNull((JSONObject) value); } } } /** * 处理JSONArray对象 */ private void handleJSONArray(JSONArray array) { for (int i = 0; i < array.size(); i++) { Object item = array.get(i); if (item == null || "null".equals(item)) { array.set(i, ""); } else { iterativeReplaceNull(item); } } }
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值