Gson implementation: modify the value corresponding to a key in the json string, how to modify a key in the json string

Modify a key in the json string

public static JsonElement replaceKey(JsonElement source, Map<String, String> rep) {
    
    
    if (source == null || source.isJsonNull()) {
    
    
        return JsonNull.INSTANCE;
    }
    if (source.isJsonPrimitive()) {
    
    
        return source;
    }
    if (source.isJsonArray()) {
    
    
        JsonArray jsonArr = source.getAsJsonArray();
        JsonArray jsonArray = new JsonArray();
        jsonArr.forEach(item -> {
    
    
            jsonArray.add(replaceKey(item, rep));
        });
        return jsonArray;
    }
    if (source.isJsonObject()) {
    
    
        JsonObject jsonObj = source.getAsJsonObject();
        Iterator<Entry<String, JsonElement>> iterator = jsonObj.entrySet().iterator();
        JsonObject newJsonObj = new JsonObject();
        iterator.forEachRemaining(item -> {
    
    
            String key = item.getKey();
            JsonElement value = item.getValue();
            if (rep.containsKey(key)) {
    
    
                String newKey = rep.get(key);
                key = newKey;
            }
            newJsonObj.add(key, replaceKey(value, rep));
        });

        return newJsonObj;
    }
    return JsonNull.INSTANCE;
}

Modify the value corresponding to a key in the json string

public static JsonElement replaceJsonNode(JsonElement jsonElement, Map<String, Object> map) {
    
    

    if (map.size() == 0) {
    
    
        return jsonElement;
    }

    // 判断如果是简单json串直接返回
    if (jsonElement.isJsonPrimitive()) {
    
    
        return jsonElement;
    }

    // 判断如果是数组类型字符串,则逐个解析
    if (jsonElement.isJsonArray()) {
    
    
        JsonArray jsonArray = jsonElement.getAsJsonArray();
        JsonArray jsonArryNew = new JsonArray();
        for (JsonElement element : jsonArray) {
    
    
            // 递归调用
            jsonArryNew.add(replaceJsonNode(element, map));
        }
        return jsonArryNew;
    }

    // 判断如果是key-value类型的
    if (jsonElement.isJsonObject()) {
    
    
        JsonObject object = jsonElement.getAsJsonObject();
        JsonObject objectNew = new JsonObject();
        for (String key : object.keySet()) {
    
    
            // 如果和目标字段匹配则更换value;
            if (map.containsKey(key)) {
    
    
                Object newValue = map.get(key);
                if (newValue instanceof String) {
    
    
                    object.addProperty(key, (String) newValue);
                } else if (newValue instanceof Boolean) {
    
    
                    object.addProperty(key, (Boolean) newValue);
                } else if (newValue instanceof Character) {
    
    
                    object.addProperty(key, (Character) newValue);
                } else {
    
    
                    object.addProperty(key, (Number) newValue);
                }
            }
            JsonElement jsonEle = object.get(key);
            JsonElement jsonElementNew = replaceJsonNode(jsonEle, map);
            objectNew.add(key, jsonElementNew);
        }
        return objectNew;
    }
    return jsonElement;
}

test

public static void main(String[] args) {
    
    
    String json = "{\"order_sn\":\"14031000273822\",\"carriers_code\":1100000357,\"carrier\":\"浙江派尔快递\",\"package_type\":2,\"packages\":[{\"0\":{\"good_sn\":\"ALM2236W36\",\"amount\":\"2\"},\"1\":{\"good_sn\":\"ALM2236W37\",\"amount\":\"2\"},\"transport_no\":\"test5715A\"},{\"0\":{\"good_sn\":\"ALM2236W35\",\"amount\":\"2\"},\"transport_no\":\"test5715B\"}]}";
    JsonElement jsonEle = new JsonParser().parse(json);
    HashMap<String, String> rep = new HashMap<>();
    rep.put("order_sn", "order_id");
    rep.put("carriers_code", "carrier_code");
    rep.put("good_sn", "barcode");
    JsonElement replaceKey = replaceKey(jsonEle, rep);
    System.out.println(replaceKey.toString());

    Map<String, Object> map = new HashMap();
    map.put("order_sn","00000000");
    map.put("carriers_code","9999999");
    map.put("good_sn",888);
    System.out.println(replaceJsonNode(jsonEle, map).toString());
}

result

  1. Before running
    {
          
          
        "order_sn": "14031000273822",
        "carriers_code": 1100000357,
        "carrier": "浙江派尔快递",
        "package_type": 2,
        "packages": [
            {
          
          
                "0": {
          
          
                    "good_sn": "ALM2236W36",
                    "amount": "2"
                },
                "1": {
          
          
                    "good_sn": "ALM2236W37",
                    "amount": "2"
                },
                "transport_no": "test5715A"
            },
            {
          
          
                "0": {
          
          
                    "good_sn": "ALM2236W35",
                    "amount": "2"
                },
                "transport_no": "test5715B"
            }
        ]
    }
    
  2. After running
    • Modify key
      {
              
              
          "order_id": "14031000273822",
          "carrier_code": 1100000357,
          "carrier": "浙江派尔快递",
          "package_type": 2,
          "packages": [
              {
              
              
                  "0": {
              
              
                      "barcode": "ALM2236W36",
                      "amount": "2"
                  },
                  "1": {
              
              
                      "barcode": "ALM2236W37",
                      "amount": "2"
                  },
                  "transport_no": "test5715A"
              },
              {
              
              
                  "0": {
              
              
                      "barcode": "ALM2236W35",
                      "amount": "2"
                  },
                  "transport_no": "test5715B"
              }
          ]
      }
      
    • Modify the value of the corresponding key
      {
              
              
          "order_sn": "00000000",
          "carriers_code": "9999999",
          "carrier": "浙江派尔快递",
          "package_type": 2,
          "packages": [
              {
              
              
                  "0": {
              
              
                      "good_sn": 888,
                      "amount": "2"
                  },
                  "1": {
              
              
                      "good_sn": 888,
                      "amount": "2"
                  },
                  "transport_no": "test5715A"
              },
              {
              
              
                  "0": {
              
              
                      "good_sn": 888,
                      "amount": "2"
                  },
                  "transport_no": "test5715B"
              }
          ]
      }
      

Guess you like

Origin blog.csdn.net/yyuggjggg/article/details/129371549