替换由JSONArray组成的JSON对象中的所有键

时间:2018-05-22 10:42:51

标签: java json

我有一个JSONObject,它由针对某些键的JSONArrays组成。

以下是JSONObject示例:

输入:

{"UserKey":"DLPAgent","Operation":"DLPRuleMatch","OrganizationId":"2f6cb1a6-ecb8-4578-b680-bf84ded07ff4","IncidentId":"2b2f39ad-84ec-4dce-5800-08d5bee87822","Workload":"OneDrive","SensitiveInfoDetectionIsIncluded":false,"RecordType":11,"Version":1,"UserId":"DLPAgent","CreationTime":"2018-05-21T06:46:18","SharePointMetaData":{"UniqueID":"5710471b-f370-4f0d-be43-f6ba83645137","SiteCollectionGuid":"1692891b-2a42-431e-befa-1da656ce5ec8","SiteCollectionUrl":"https://emumbapk-my.sharepoint.com/personal/qa_emumbapk_onmicrosoft_com","FileName":"Classified.docx","FilePathUrl":"https://emumbapk-my.sharepoint.com/personal/qa_emumbapk_onmicrosoft_com/Documents/Classified.docx","FileOwner":"qa@emumbaPK.onmicrosoft.com","From":"qa@emumbaPK.onmicrosoft.com","ItemCreationTime":"2018-04-24T11:40:09","ItemLastModifiedTime":"2018-04-24T11:40:23"},"PolicyDetails":[{"PolicyName":"My policy","Rules":[{"Actions":["NotifyUser"],"RuleId":"61e36207-5ad7-4bc2-94f0-7f8b207e142c","RuleMode":"Enable","ConditionsMatched":{"SensitiveInformation":[{"Confidence":75,"Count":1,"SensitiveType":"a2ce32a8-f935-4bb6-8e96-2a5157672e2c"},{"Confidence":85,"Count":1,"SensitiveType":"e55e2a32-f92d-4985-a35d-a0b269eb687b"},{"Confidence":94,"Count":1,"SensitiveType":"a44669fe-0d48-453d-a9b1-2cc83f2cba77"}]},"Severity":"Low","RuleName":"Low volume of content detected My policy"}],"PolicyId":"82111a23-de2c-418f-b052-67e1ef639100"}],"Id":"73734ce7-fb54-41ab-ec00-08d5bee68e61","UserType":4}

我想用保留值的自己的键替换所有键。

例如,我想将UserKey替换为user_key。我怎么能这样做?

我使用简单的JSONObject

JSONObject.put("my_key" , JSONObject.get("OriginalKey"));

但是现在我无法使用JSONObject.get("OriginalKey")来获取密钥的JSONArray值。

2 个答案:

答案 0 :(得分:1)

public class TestMain {
    public static void main(String[] args) throws Exception {
        JSONObject requiredJSONObject = performJSONObject("Your JSONObject");
    }

    public static JSONObject performJSONObject(JSONObject inputObject) throws Exception {
        JSONObject resultObject = new JSONObject();
        Iterator iterator = inputObject.keys();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            if (inputObject.get(key) instanceof JSONObject) {
                JSONObject jsonObject = (JSONObject) inputObject.get(key);
                resultObject.put(getModifiedKey(key), performJSONObject(jsonObject));
            } else if (inputObject.get(key) instanceof JSONArray) {
                JSONArray jsonArray = (JSONArray) inputObject.get(key);
                resultObject.put(getModifiedKey(key), performJSONArray(jsonArray));
            } else {
                resultObject.put(getModifiedKey(key), inputObject.get(key));
            }
        }
        return resultObject;
    }

    public static JSONArray performJSONArray(JSONArray inputArray) throws Exception {
        JSONArray resultArray = new JSONArray();
        for (int i = 0; i < inputArray.length(); i++) {
            if (inputArray.get(i) instanceof JSONObject) {
                JSONObject jsonObject = (JSONObject) inputArray.get(i);
                resultArray.put(i, performJSONObject(jsonObject));
            } else if (inputArray.get(i) instanceof JSONArray) {
                JSONArray jsonArray = (JSONArray) inputArray.get(i);
                resultArray.put(i, performJSONArray(jsonArray));
            } else {
                resultArray.put(i, inputArray.get(i));
            }
        }
        return resultArray;
    }

    public static String getModifiedKey(String strn) {
        String[] r = strn.split("(?=\\p{Upper})");
        String result = "";
        for (String str : r)
            result += str.toLowerCase() + "_";
        return result.substring(0, result.length() - 1);
    }
}

这里我们需要使用递归,上面的解决方案可能对你有帮助。感谢。

答案 1 :(得分:0)

使用以下方法获取修改后的密钥

public static String getModifiedKey(String strn) {
            String[] r = strn.split("(?=\\p{Upper})");
            String result = "";
            for (String str : r)
                result += str.toLowerCase() + "_";
            return result.substring(0, result.length()-1);
        }

使用下面的一个来插入数据

JSONObject.put(getModifiedKey(OriginalKey) , JSONObject.get("OriginalKey"));