如何通过使用正则表达式修改其中的几个字段来创建新的JSON字符串? [英] How to create a new JSON string by modifying few fields in it using Regular Expressions?
问题描述
我正在使用JSON,我将拥有这样的JSON-我将其称为originalJsonResponse
.下面所有的json都是相同的,只是user_id
和uid
字段可能具有这些类型的变体-
I am working with JSON and I will have JSON like this - I called it as originalJsonResponse
. All the below json are same, it's just that user_id
and uid
field might have these types of variations -
{ "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
字段(如果存在),并且如果这些字段的值不为null,则将这些字段的值更改为新的用户ID,然后构造另一个具有新用户ID的新json.
Now I need to extract user_id
or uid
field from the JSON (if it is present) and if the value of those fields are not null, then change the value of those fields to a new user id and then construct another new json with the new user id in it.
作为示例,我将newUserId
作为1267818821
,那么我的新json应该看起来像这样,新json应该具有与旧json相同的结构-
As an example, I will have newUserId
as 1267818821
, then my new json should look like this and new json should have the same structure as old 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
字段进行混淆的最佳方法是什么?
What is the best way to do this obfuscation of user_id
and uid
field?
- 我可以在这里使用正则表达式进行混淆吗?
- 我能想到的其他方法是使用Gson解析JSON,但是如果我走这条路线,那么GSON会将传入内容读取为Double,并且读取时我的值可能会发生变化.
下面是我要进行混淆处理的方法-
Below is my method in which I want to do the obfuscation -
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
编号,所以我没有要替换那些.有更好的方法吗?
I cannot use replaceAll
method here (that's what I have started with and realize it will not work) since I might have another json with different fields and those fields might have oldUserId
number in it so I don't want to replace those. Is there any better way of doing this?
// not a good solution
String newJsonResponse = originalJsonResponse.replaceAll(String.valueOf(oldUserId), String.valueOf(newUserId));
我想使它更通用,以便将来如果我想替换其他字段而不是user_id
和uid
字段,那么我应该可以轻松地做到这一点.
I want to make this more generic so that in future if I want to replace some other fields instead of user_id
and uid
field, then I should be able to do it easily.
推荐答案
由于您在此处共享了user_id
字段的所有可能的输入JSON字符串模式,因此,我想再次介绍我提出的JSON解析器解决方案之前,但现在已更新为可以处理您的不同JSON类型.
Since you've shared all the possible input JSON string patterns for the user_id
field here, I would like to present again my JSON parser solution proposed before but updated to handle your different JSON types now.
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;
}
}
输出:
Output :
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字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!