转义字符已添加到JSON对象

时间:2019-01-09 07:04:57

标签: java json apache-commons

我用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"}

谢谢。

2 个答案:

答案 0 :(得分:1)

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

此行为是错误的。我建议您使用其他JSON库。

答案 1 :(得分:-1)

问题是您使用的unescapeXML的工作方式略有不同,以转义JSON中的字符。我相信您应该在println中使用unescapeJSON