因此,您需要解决的实际问题是换行.您可以这样禁用它: DumperOptions options = new DumperOptions();options.setSplitLines(false);Yaml yaml = new Yaml(options);System.out.println(yaml.dump(yamlMap)); 请注意,带有换行符&的YAML转义空间确实可以正确加载.尽量不要对YAML表示形式提出过多的要求,因为您无法完全控制它.i am trying to read a Yaml template and replace certain fields in the template dynamically and create a new Yaml file using Snake Yaml. But I am getting escape space character if a String contains spacess for the required fields when I use snake yaml. Can anyone please suggest to resolve this issue?I am getting one more issue from map to yaml conversion with spaces.Example :--------------------------------version: snapshot-01kind: samplemetadata: name: abcoptions: "<placeholder>"--------------------------------I am reading the above template and replacing the required fields dynamically as shown below. Yaml yaml = new Yaml(); InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(yamlTemplateLocation); Map<String, Object>yamlMap = yaml.load(inputStream);When i replace the place holders with a string which contain spaces Ex : "abc sfksajfkl jfajfkjakj jqjlkkalkl kajklfjalkd"yamlMap.put("version","v-1.0");yamlMap.put("options","abc sfksajfkl jfajfkjakj jqjlkkalkl kajklfjalkd");I am getting output as--------------------------------version: "v-1.0"kind: samplemetadata: name: abcoptions: "abc sfksajfkl jfajfkjakj \ jqjlkkalkl kajklfjalkd "--------------------------------Note : It is generating escape space character (i.e "\ ")in "abc sfksajfkl jfajfkjakj \ jqjlkkalkl kajklfjalkd "But my requirement is like below - which should not generate any escape space character--------------------------------version: "v-1.0"kind: samplemetadata: name: abcoptions: "abc sfksajfkl jfajfkjakj jqjlkkalkl kajklfjalkd"--------------------------------Could anyone please help me with this? Thanks in advance! 解决方案 The code you show doesn't match the YAML output since the code only has single spaces. I will assume the content in the YAML is the actual string being put in the map since the code you show would not produce this YAML and your requirement also include the double spaces.The escape character is just a side effect of line breaking. When breaking a quoted scalar over multiple lines, line breaks are folded into single spaces. However, you have multiple spaces between the words, so YAML must insert an escaped space to mark the second space as content (all non-escaped spaces at the beginning of the second line is considered indentation and not part of the content).So the actual problem you need to solve is line breaking. You disable it like this:DumperOptions options = new DumperOptions();options.setSplitLines(false);Yaml yaml = new Yaml(options);System.out.println(yaml.dump(yamlMap));As a side note, the YAML with line breaks & escaped space does load correctly. Try not to put too specific requirements on YAML representation since you cannot completely control it. 这篇关于从地图转换为Yaml时,Snake Yaml Dumper选项会为带有空格的字符串生成不必要的转义符("\")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!