json字符串的替换功能

 

处理json字符串:

String content = "{json格式字符串}";
Map map = new HashMap<>();
Map map1 = JsonParser.fromJson(content,Map.class);
Map action =(Map)map1.get("action");
Map value =(Map)map1.get("value");
System.out.println(action);
System.out.println(value);
String title = (String) action.get("title");
String context = (String) action.get("content");
System.out.println(context+" ,"+title);
for(Map.Entry i : value.entrySet()){
    map.put(i.getKey(),(String)i.getValue());
}
System.out.println(map);
for(Map.Entry s : renderString(title,context,map).entrySet()){
    System.out.println(s);
}

json字符串替换function:

public static Map renderString(String title,String content, Map map) {
    Map m = new HashMap<>();
    Set> sets = map.entrySet();
    for (Map.Entry entry : sets) {
        String regex = "\\$\\{" + entry.getKey() + "}";
        System.out.println(regex);
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        content = matcher.replaceAll(entry.getValue());
        Matcher matcher1 = pattern.matcher(title);
        title = matcher1.replaceAll(entry.getValue());
    }
    m.put("title",title);
    m.put("content",content);
    return m;
}

 

 

 

你可能感兴趣的:(json字符串的替换功能)