gpt4 book ai didi

java - JAVA中无法转义双引号

转载 作者:行者123 更新时间:2023-12-01 16:41:43 25 4
gpt4 key购买 nike

当我尝试将 map 转换为字符串时,我正在努力转义双引号

//iterate through map and append to array
StringBuilder mapAsString = new StringBuilder("{");
for (String key : billingDetails.keySet()) {
mapAsString.append(key + ":" + billingDetails.get(key) + ", ");
}
mapAsString.delete(mapAsString.length()-2, mapAsString.length()).append("}");
String bdResult = mapAsString.toString();

电流输出

"position": [
"{2018:Element3, 2012:Element2, 2010:Element1}"

预期输出

"position": [
"{"2018":"Element3", "2012":"Element2", "2010":"Element1"}"

以下方法将映射的内容附加到字符串,但是我似乎无法弄清楚如何转义引号以使预期输出正确

 mapAsString.append(key + ":" + billingDetails.get(key) + ", ");

在java中转义字符对我来说一直有些困难,如果有更广泛知识的人能帮助我,我将不胜感激

干杯和感谢

最佳答案

用反斜杠转义引号

mapAsString.append("\"" + key + "\":\"" + billingDetails.get(key) + "\", ");

关于java - JAVA中无法转义双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61849160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com