xyc
2024-08-02 7abc95a191d2c1a9bb9ff8fd7fb05470a0d4e86b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.zt.common.utils;
 
import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.apache.commons.lang3.ArrayUtils;
 
public class JsonUtils2 {
 
    /*    public static boolean setArrJsonValueByPath(JSONObject pJSONObject, String[] path, String searchKey, String searchValue, String targetKey, Object value) {
            if (path.length == 1) {
                JSONArray jsonArray = pJSONObject.getJSONArray(path[0]);
                for (int i = 0; i < jsonArray.size(); i++
                ) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    if (jsonObject.get(searchKey).equals(searchValue)) {
                        jsonObject.put(targetKey, value);
                    }
                }
                return true;
            } else {
                JSONObject sJSONObject = pJSONObject.getJSONObject(path[0]);
                path = ArrayUtils.remove(path, 0);
                setArrJsonValueByPath(sJSONObject, path, searchKey, searchValue, targetKey, value);
            }
            return false;
        }*/
    public static boolean setArrJsonValueByPath(JSONObject pJSONObject, String[] path, String searchKey, String searchValue, String path2, Object value) {
        return setArrJsonValueByPath(pJSONObject, path, searchKey, searchValue, path2.split("/"), value);
 
    }
 
    public static boolean setArrJsonValueByPath(JSONObject pJSONObject, String[] path, String searchKey, String searchValue, String[] path2, Object value) {
        if (path.length == 1) {
            JSONArray jsonArray = pJSONObject.getJSONArray(path[0]);
            JSONObject targetJSONObject = null;
            if (searchKey != null) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    if (jsonObject.get(searchKey).equals(searchValue)) {
                        targetJSONObject = jsonObject;
                    }
                }
            } else {
                targetJSONObject = jsonArray.getJSONObject(0);
            }
            setJsonValueByPath(targetJSONObject, path2, value);
            return true;
        } else {
            JSONObject sJSONObject = pJSONObject.getJSONObject(path[0]);
            path = ArrayUtils.remove(path, 0);
            setArrJsonValueByPath(sJSONObject, path, searchKey, searchValue, path2, value);
        }
        return false;
    }
 
    public static Object getArrJsonValueByPath(JSONObject pJSONObject, String[] path, String searchKey, String searchValue, String targetKey) {
        if (path.length == 1) {
            JSONArray jsonArray = pJSONObject.getJSONArray(path[0]);
            for (int i = 0; i < jsonArray.size(); i++
            ) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                if (jsonObject.get(searchKey).equals(searchValue)) {
                    return jsonObject.get(targetKey);
                }
            }
            return null;
        } else {
            JSONObject sJSONObject = pJSONObject.getJSONObject(path[0]);
            path = ArrayUtils.remove(path, 0);
            return getArrJsonValueByPath(sJSONObject, path, searchKey, searchValue, targetKey);
        }
    }
 
    public static boolean setJsonValueByPath(JSONObject pJSONObject, String[] path, Object value) {
        if (path.length == 1) {
            pJSONObject.put(path[0], value);
            return true;
        } else {
            JSONObject sJSONObject = pJSONObject.getJSONObject(path[0]);
            if (sJSONObject == null) {
                sJSONObject = new JSONObject("{}");
                pJSONObject.put(path[0], sJSONObject);
            }
            path = ArrayUtils.remove(path, 0);
            setJsonValueByPath(sJSONObject, path, value);
        }
        return false;
    }
 
    public static Object getJsonValueByPath(JSONObject pJSONObject, String[] path) {
        if (path.length == 1) {
            return pJSONObject.get(path[0]);
        } else {
            JSONObject sJSONObject = pJSONObject.getJSONObject(path[0]);
            if (sJSONObject == null)
                return null;
            path = ArrayUtils.remove(path, 0);
            return getJsonValueByPath(sJSONObject, path);
        }
    }
}