Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 取代;环境变量“;在字符串中,值存储在JSON对象中。(爪哇)_Java_Json_String_Replace_Matcher - Fatal编程技术网

Java 取代;环境变量“;在字符串中,值存储在JSON对象中。(爪哇)

Java 取代;环境变量“;在字符串中,值存储在JSON对象中。(爪哇),java,json,string,replace,matcher,Java,Json,String,Replace,Matcher,假设a有一个如下字符串: String str = "Lorem Ipsum Neque porro <userEnv.value1> quisquam est qui <userEnv.value2> dolorem ipsum quia <userEnv.value2> dolor sit amet, consectetur, adipisci velit.."; "userEnv":{

假设a有一个如下字符串:

String str = "Lorem Ipsum Neque porro <userEnv.value1> quisquam est qui <userEnv.value2> dolorem
              ipsum quia <userEnv.value2> dolor sit amet, consectetur, adipisci velit..";
"userEnv":{
     "value1":"important info",
     "value2":"other important info",
     "value3":"even other important info"
}
我需要找到一种聪明的方法来替换JSON userEnv中存储的字符串中的每个值

我考虑做如下事情:

        JsonObject jsonObject = Json.createObject();
        jsonObject.put(
            "userEnv",
            "\"value1\":\"important info\",\"value2\":\"other important info\",\"value3\":\"even other important info\");
        
        Pattern pattern = Pattern.compile(".*[<](.*)[>].*");
        Matcher matcher = pattern.matcher(str);

        int groupPointer = 0;
        while (matcher.find()){

            if (jsonObject.get(matcher.group(groupPointer))!=null){
                str = str.replaceAll(
                           ".*<"+matcher.group(groupPointer)+">.*",
                           jsonObject.get(matcher.group(groupPointer))
                 );
            }
        }
JsonObject JsonObject=Json.createObject();
jsonObject.put(
“userEnv”,
“\'value1\”:“重要信息”、\'value2\”:“其他重要信息”、\'value3\”:“甚至其他重要信息”;
Pattern=Pattern.compile(“.[].]”);
Matcher-Matcher=pattern.Matcher(str);
int-groupPointer=0;
while(matcher.find()){
if(jsonObject.get(matcher.group(groupPointer))!=null){
str=str.replaceAll(
".*.*",
jsonObject.get(matcher.group(groupPointer))
);
}
}
但这不起作用,没有找到任何团体,也许我的正则表达式表达得不好

另外,我不确定JSON对象的创建

也许我应该明天发布,而不是今晚发布。使用org.json库中的JSONObject,您可以执行以下操作
    //using JSONObject from org.json library you could do the following
    JSONObject jsonObject = new JSONObject("{\"userEnv\":{\n" +
            "     \"value1\":\"important info\",\n" +
            "     \"value2\":\"other important info\",\n" +
            "     \"value3\":\"even other important info\"\n" +
            "}}");
    String str = "Lorem Ipsum Neque porro <userEnv.value1> quisquam est qui <userEnv.value2> dolorem " +
            "ipsum quia <userEnv.value2> dolor sit amet, consectetur, adipisci velit..";
    for (String outerKey : jsonObject.keySet()) {
        JSONObject outerKeyValue = jsonObject.getJSONObject(outerKey);
        for (String innerKey : outerKeyValue.keySet()) {
            String innerKeyValue = outerKeyValue.getString(innerKey);
            str = str.replace("<" + outerKey + "." + innerKey + ">", innerKeyValue);
        }
    }
    //here str has replaced all placeHolders of type <outerKey.innerKey> for which there were values in the json
JSONObject JSONObject=新的JSONObject(“{\“userEnv\”:{\n”+ “\'value1\':\'maintal info\',\n”+ “\'value2\':\'其他重要信息\',\n”+ “\“value3\”:\“甚至其他重要信息\”\n”+ "}}"); String str=“Lorem Ipsum Neque porro quisquam est qui dolorem”+ “我的爱,我的爱,我的爱”; for(字符串outerKey:jsonObject.keySet()){ JSONObject outerKeyValue=JSONObject.getJSONObject(outerKey); for(字符串innerKey:outerKeyValue.keySet()){ 字符串innerKeyValue=outerKeyValue.getString(innerKey); str=str.replace(“,innerKeyValue); } } //这里str替换了json中有值的所有类型的占位符 这就是它的用途:


谢谢你的回答。我使用了StringSubstitutor,效果非常好。我在问题中也提到了Json树有点复杂,所以我以前需要展平所有键。如果你觉得答案有用,你可以向上投票:)感谢你的回答,我意识到我需要提前展平。我使用了另一种方法,因为Json树比2个键更深(或更高)。谢谢!
Gson gson = new Gson();
JsonObject json = gson.fromJson(jsonStr, JsonObject.class);
Map<String, String> map = gson.fromJson(json.getAsJsonObject("userEnv"), new TypeToken<Map<String, String>>(){}.getType());

Map<String, String> placeHolderToValue = map.entrySet().stream() // adding "userEnv."
        .collect(Collectors.toMap(key -> "userEnv." + key.getKey(), Map.Entry::getValue));

StringSubstitutor sub = new StringSubstitutor(placeHolderToValue, "<", ">");
String result = sub.replace(str);

System.out.println(result);
Lorem Ipsum Neque porro important info quisquam est qui other important info dolorem
              ipsum quia other important info dolor sit amet, consectetur, adipisci velit..