转义字符被添加到 JSON 对象

Escape characters are getting added to JSON object

我用 json 字符串创建了一个 json 对象。创建对象后,我正在操纵它的值。如果我的值包含 html 标签,如“</p>”,则会将其转换为“<\/p>”。

有没有办法避免呢。这是我的代码

import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import com.cloudwords.org.apache.commons.lang3.StringEscapeUtils;

public class EscapeText {
    public static void main(String[] args) throws JSONException {
        JSONObject jsonObj = new JSONObject("{\"text\": \"value11\",\"rte\": \"this <p>is</p> RTE\"}");
        jsonObj.put("rte", "Diese<p>ist</p>RTE");
        jsonObj.put("text", "value11");
        System.out.println(StringEscapeUtils.unescapeXml(jsonObj.toString()));
    }
}

输出是:- {"text":"value11","rte":"Diese<p>ist<\/p>RTE"} 预期输出:- {"text":"value11","rte":"Diese<p>ist</p>RTE"}

谢谢。

问题是您使用的 unescapeXML 对 JSON 中的字符进行转义的方式略有不同。我相信你应该在你的 println 中使用 unescapeJSON

此行为在 JSONStringer.string(String) 方法中进行了描述。

这种行为是错误的。我建议你使用不同的 JSON 库。