java的json操作总结

1.字符串转json数组

JSONArray jsonArray = JSON.parseArray(quotationValue);

2.循环遍历json数组

jsonArray.forEach(json -> {
                    JSONObject jsonObject = sortedArray((JSONObject) json);
                    for (String key : jsonObject.keySet()) {
                        Object value = jsonObject.get(key);
                        if (stageId.equals(value)) {
                            LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();
                            TreeMap<String, Object> objectTreeMap = sortedMap(jsonObject, filterStructureKeyAndValue);
                            for (String treeKey : objectTreeMap.keySet()) {
                                linkedHashMap.put(treeKey, objectTreeMap.get(treeKey));
                            }
                            linkedHashMapList.add(linkedHashMap);
                        }
                        break;
                    }
                });

3.获取JSONObject的键值对

jsonArray.forEach(json -> {
                    JSONObject jsonObject = sortedArray((JSONObject) json);
                    for (String key : jsonObject.keySet()) {
                        Object value = jsonObject.get(key);
                        if (stageId.equals(value)) {
                            LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();
                            TreeMap<String, Object> objectTreeMap = sortedMap(jsonObject, filterStructureKeyAndValue);
                            for (String treeKey : objectTreeMap.keySet()) {
                                linkedHashMap.put(treeKey, objectTreeMap.get(treeKey));
                            }
                            linkedHashMapList.add(linkedHashMap);
                        }
                        break;
                    }
                });

4.组装json的报价数据

/**
     * @param array1 产品管理里面最长数组长度的一条数据
     * @param array2 产品管理数据
     * @return 组装所有报价的数据到一个map,比如 起订量(10)、起订量(15)、起订量(20)、起订量(30)
     */
    public LinkedHashMap<String, Object> getLinkedHashMap(JSONArray array1, JSONArray array2) {
        // 组装26个字母
        TreeMap<Integer, Character> alphabetMap = new TreeMap<>();
        for (int key = 1; key <= 26; key++) {
            char alphabet = (char) ('A' + key - 1); // Assuming uppercase alphabets
            alphabetMap.put(key, alphabet);
        }

        LinkedHashMap<String, Object> linkedSpecialHashMap = new LinkedHashMap<>();
        int maxDigits = array1.size(); // 限制最大位数为两位

        for (int i = 0; i < maxDigits; i++) {
            String value1 = array1.getString(i);
            TreeMap<String, Object> objectTreeMap = sortedMap2(JSON.parseObject(value1));
            String key = null;
            String value = null;
            for (String key2 : objectTreeMap.keySet()) {
                key = key2;
                value = (String) objectTreeMap.get(key2);
                break;
            }
            boolean isAdd = true;
            boolean isAdd2 = false;
            for (int j = 0; j < array2.size(); j++) {
                String value2 = array2.getString(j);
                TreeMap<String, Object> objectTreeMap1 = sortedMap2(JSON.parseObject(value2));
                for (String key3 : objectTreeMap1.keySet()) {
                    String value3 = (String) objectTreeMap1.get(key3);
                    isAdd = key != null && value != null && key.equals(key3) && value.equals(value3);
                    break;
                }
                if (isAdd) {
                    for (String key3 : objectTreeMap1.keySet()) {
                        Object value4 = objectTreeMap1.get(key3);
                        linkedSpecialHashMap.put(key3 + alphabetMap.get(i + 1), value4);
                    }
                    isAdd2 = true;
                }
            }
            if (!isAdd && !isAdd2) {
                for (String key2 : objectTreeMap.keySet()) {
                    linkedSpecialHashMap.put(key2 + alphabetMap.get(i + 1), "-");
                }
            }
        }
        return linkedSpecialHashMap;
    }

5、json排序

/**
     * json排序
     *
     * @param jsonObject json
     * @return 排序后的结果
     */
    public static TreeMap<String, Object> sortedMap2(JSONObject jsonObject) {
        return new TreeMap<>(sortedArray(jsonObject));
    }
/**
     * json排序
     *
     * @param jsonObject json
     * @return 排序后的结果
     */
    public TreeMap<String, Object> sortedMap(JSONObject jsonObject, TreeMap<String, QuotationStructureVo> filterStructureKeyAndValue) {
        List<String> keyList = new ArrayList<>(jsonObject.keySet());
        for (Map.Entry<String, QuotationStructureVo> entryKey : filterStructureKeyAndValue.entrySet()) {
            QuotationStructureVo quotationStructureVo = entryKey.getValue();
            if (!keyList.contains(quotationStructureVo.getComponentKey())) {
                jsonObject.put(quotationStructureVo.getComponentKey(), "-");
            }
        }
        return new TreeMap<>(sortedArray(jsonObject));
    }
/**
     * json排序
     *
     * @param jsonObject 参数
     * @return 结果
     */
    public static JSONObject sortedArray(JSONObject jsonObject) {
        // 创建一个 TreeMap,用于保存排序后的键值对
        TreeMap<String, Object> sortedMap = new TreeMap<>(jsonObject);
        // 创建一个新的 JSONObject
        return new JSONObject(sortedMap);
    }

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