如何使用正则表达式修改其中的几个字段来创建新的 JSON 字符串?
How to create a new JSON string by modifying few fields in it using Regular Expressions?
我正在与 JSON 合作,我将拥有这样的 JSON - 我将其命名为 originalJsonResponse
。以下所有 json 都是相同的,只是 user_id
和 uid
字段可能有这些类型的变化 -
{ "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_id
或 uid
字段,如果这些字段的值不为空,则更改值将这些字段添加到一个新的用户 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_id
和 uid
字段的最佳方法是什么?
- 我可以在这里使用正则表达式来进行混淆吗?
- 我能想到的其他方法是使用 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_id
和 uid
字段,那么我应该能够轻松完成。
这不是适合正则表达式的用例。
对于这种类型的东西,JSON 处理器 绝对是您想要使用的。这样你就可以反序列化传入的JSON到你定义的class中,根据需要改变值和结构,然后 序列化 它回到传出的 JSON 响应。
我使用并推荐 Jackson, but you could use GSON also if you wanted. There's also a Jackson quickstart tutorial。
您可以指定注释 @JsonInclude(Include.NON_NULL)
仅对非空值进行序列化,这样,如果 JSON 具有 user_id
但不是 uid
,null
uid
不会被序列化。
处理特定 属性 中不同变体的复杂性(例如 user_id
作为 long
或 string
或任一类型的嵌套哈希) ,最好编写 custom serializers 和反序列化程序。 GSON 也有这些。
由于您已经在此处分享了 user_id
字段的所有可能输入 JSON 字符串模式,我想再次展示我提出的 JSON 解析器解决方案 但更新后现在可以处理不同的 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"}}
我正在与 JSON 合作,我将拥有这样的 JSON - 我将其命名为 originalJsonResponse
。以下所有 json 都是相同的,只是 user_id
和 uid
字段可能有这些类型的变化 -
{ "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_id
或 uid
字段,如果这些字段的值不为空,则更改值将这些字段添加到一个新的用户 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_id
和 uid
字段的最佳方法是什么?
- 我可以在这里使用正则表达式来进行混淆吗?
- 我能想到的其他方法是使用 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_id
和 uid
字段,那么我应该能够轻松完成。
这不是适合正则表达式的用例。
对于这种类型的东西,JSON 处理器 绝对是您想要使用的。这样你就可以反序列化传入的JSON到你定义的class中,根据需要改变值和结构,然后 序列化 它回到传出的 JSON 响应。
我使用并推荐 Jackson, but you could use GSON also if you wanted. There's also a Jackson quickstart tutorial。
您可以指定注释 @JsonInclude(Include.NON_NULL)
仅对非空值进行序列化,这样,如果 JSON 具有 user_id
但不是 uid
,null
uid
不会被序列化。
处理特定 属性 中不同变体的复杂性(例如 user_id
作为 long
或 string
或任一类型的嵌套哈希) ,最好编写 custom serializers 和反序列化程序。 GSON 也有这些。
由于您已经在此处分享了 user_id
字段的所有可能输入 JSON 字符串模式,我想再次展示我提出的 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"}}