java - 如何使用正则表达式修改其中的几个字段来创建新的 JSON 字符串?

标签 java regex json

我正在使用 JSON,并且我将拥有这样的 JSON - 我将其称为 originalJsonResponse。下面的所有 json 都是相同的,只是 user_iduid 字段可能有这些类型的变化 -

{ "user_id": { "long": 159002376 }, "filter": { "string": "hello" } }
{ "user_id": { "string": "159002376" }, "filter": { "string": "hello" } }
{ "user_id": "159002376" , "filter": { "string": "hello" } }
{ "user_id": 159002376 , "filter": { "string": "hello" } }
{ "user_id": null, "filter": { "string": "hello" } }

{ "uid": { "long": 159002376 }, "filter": { "string": "hello" } }
{ "uid": { "string": "159002376" }, "filter": { "string": "hello" } }
{ "uid": "159002376" , "filter": { "string": "hello" } }
{ "uid": 159002376 , "filter": { "string": "hello" } }
{ "uid": null, "filter": { "string": "hello" } }

{ "filter": { "string": "hello" } }

现在我需要从 JSON 中提取 user_iduid 字段(如果存在),如果这些字段的值不为空,则更改值这些字段到一个新的用户 ID,然后构造另一个新的 json,其中包含新的用户 ID。

举个例子,我将 newUserId 设置为 1267818821,那么我的新 json 应该如下所示,并且新 json 应该具有与旧 json 相同的结构 -

{ "user_id": { "long": 1267818821 }, "filter": { "string": "hello" } }
{ "user_id": { "string": "1267818821" }, "filter": { "string": "hello" } }
{ "user_id": "1267818821" , "filter": { "string": "hello" } }
{ "user_id": 1267818821 , "filter": { "string": "hello" } }
{ "user_id": null, "filter": { "string": "hello" } }

{ "uid": { "long": 1267818821 }, "filter": { "string": "hello" } }
{ "uid": { "string": "1267818821" }, "filter": { "string": "hello" } }
{ "uid": "1267818821" , "filter": { "string": "hello" } }
{ "uid": 1267818821 , "filter": { "string": "hello" } }
{ "uid": null, "filter": { "string": "hello" } }

{ "filter": { "string": "hello" } }

user_iduid 字段进行混淆的最佳方法是什么?

  • 我可以在这里使用正则表达式来进行混淆吗?
  • 我能想到的其他方法是使用 Gson 解析 JSON,但如果我走这条路线,那么 GSON 将所有内容读取为 Double,并且我的值在读取时可能会发生变化。

下面是我想要进行混淆的方法 -

private static String obfuscateJson(String originalJsonResponse, String oldUserId, String newUserId) {
    // here oldUserId is 159002376 and newUserId is 1267818821

    // not sure what I should do here to construct a new json with newUserId

    String newJsonResponse = // make a new json here
    return newJsonResponse;
}

我不能在这里使用 replaceAll 方法(这就是我开始使用的方法,并意识到它不起作用),因为我可能有另一个具有不同字段的 json,并且这些字段可能有 oldUserId 其中的数字,所以我不想替换它们。有没有更好的方法来做到这一点?

// not a good solution
String newJsonResponse = originalJsonResponse.replaceAll(String.valueOf(oldUserId), String.valueOf(newUserId));

我想让这个更加通用,这样将来如果我想替换一些其他字段而不是 user_iduid 字段,那么我应该能够这样做很容易。

最佳答案

由于您已在此处共享了 user_id 字段的所有可能的输入 JSON 字符串模式,因此我想再次介绍我建议的 JSON 解析器解决方案 before但现在已更新以处理不同的 JSON 类型。

public static void main(String[] args) {
    String[][] jsonInputs = new String[6][2];

    jsonInputs[0][0] = "Long-Object";
    jsonInputs[0][1] = "{ \"user_id\":{\"long\":876},\"client_id\":{\"int\":0},\"affinity\":[{\"try\":{\"long\":55787693},\"scoring\":{\"float\":0.19}},{\"try\":{\"long\":1763},\"scoring\":{\"float\":0.0114}}]}";

    jsonInputs[1][0] = "String-Object";
    jsonInputs[1][1] = "{ \"user_id\":{\"string\": \"876\"},\"client_id\":{\"int\":0},\"affinity\":[{\"try\":{\"long\":55787693},\"scoring\":{\"float\":0.19}},{\"try\":{\"long\":1763},\"scoring\":{\"float\":0.0114}}]}";

    jsonInputs[2][0] = "String";
    jsonInputs[2][1] = "{ \"user_id\": \"1267818821\" , \"filter\": { \"string\": \"hello\" } }";

    jsonInputs[3][0] = "Long";
    jsonInputs[3][1] = "{ \"user_id\": 1267818821 , \"filter\": { \"string\": \"hello\" } }";

    jsonInputs[4][0] = "Null";
    jsonInputs[4][1] = "{ \"user_id\": null , \"filter\": { \"string\": \"hello\" } }";

    jsonInputs[5][0] = "Not-Present";
    jsonInputs[5][1] = "{ \"filter\": { \"string\": \"hello\" } }";

    for (String[] json : jsonInputs) {
        System.out.println(json[0]);
        System.out.println(changeJsonString(json[1], "54321"));
        System.out.println();
    }
}

private static String changeJsonString(String originalResponse, String newId) {
    try {
        JSONObject root = new JSONObject(originalResponse);
        if (!root.isNull("user_id")) {
            Object userObj = root.get("user_id");
            if (userObj instanceof JSONObject) {
                JSONObject userId = (JSONObject) userObj;
                if (userId.has("long")) {
                    userId.put("long", Long.parseLong(newId));
                } else {
                    userId.put("string", newId);
                }
            } else if (userObj instanceof Number) {
                root.put("user_id", Long.parseLong(newId));
            } else {
                root.put("user_id", newId);
            }
        }
        return root.toString();
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
}

输出:

Long-Object
{"user_id":{"long":54321},"client_id":{"int":0},"affinity":[{"scoring":{"float":0.19},"try":{"long":55787693}},{"scoring":{"float":0.0114},"try":{"long":1763}}]}

String-Object
{"user_id":{"string":"54321"},"client_id":{"int":0},"affinity":[{"scoring":{"float":0.19},"try":{"long":55787693}},{"scoring":{"float":0.0114},"try":{"long":1763}}]}

String
{"filter":{"string":"hello"},"user_id":"54321"}

Long
{"filter":{"string":"hello"},"user_id":54321}

Null
{"filter":{"string":"hello"},"user_id":null}

Not-Present
{"filter":{"string":"hello"}}

关于java - 如何使用正则表达式修改其中的几个字段来创建新的 JSON 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27914149/

相关文章:

python - re.search 返回空元组

ios - Int64 不工作,而 Int 工作

java - 在Android中获取字体字符间距

javascript - 使用 Javascript 进行磁盘文本处理

html - 用于匹配主题标签的正则表达式模式,但不在 HTML 属性中

c# - 使用 Newtonsoft.Json 反序列化 DbGeometry

java - android中使用json进行http连接时出错

java - 了解接口(interface)当前类路径中是否有实现类

java - 从 ADT 模拟器报告堆栈跟踪

java - 处理浮点比较中的相等情况