如何在java中使用内部HTML标记解析json?

如何在java中使用内部HTML标记解析json?,java,html,json,xml,Java,Html,Json,Xml,我从XML文件创建一个JSON字符串。现在在我的JSON中,有一些HTML标记使其无效。我使用common.lang3对HTML进行编码,但仍然存在问题。我不想删除或替换HTML标记 JSONArray newsJSONArray = new JSONArray(); int start = 0; int end = 5; List<NewsModel> newsList = NewsDAO.get(start, end); for (New

我从XML文件创建一个JSON字符串。现在在我的JSON中,有一些HTML标记使其无效。我使用common.lang3对HTML进行编码,但仍然存在问题。我不想删除或替换HTML标记

    JSONArray newsJSONArray = new JSONArray();
    int start = 0;
    int end = 5;
    List<NewsModel> newsList = NewsDAO.get(start, end);

    for (NewsModel newsModel : newsList) {
        JSONObject newsJSONObj = new JSONObject();

        String trueContent = newsModel.getContent();

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db;
        Document doc;
        try {
            db = dbf.newDocumentBuilder();
            doc = db.parse(new InputSource(new StringReader(trueContent)));
            XPath xpath = XPathFactory.newInstance().newXPath();
            String output = (String) xpath.compile("root/static-content")
                    .evaluate(doc, XPathConstants.STRING);

            String content = StringEscapeUtils.escapeHtml3(output);
            newsJSONObj.put("title", newsModel.getTitle());
            newsJSONObj.put("displayDate", newsModel.getDisplayDate());
            newsJSONObj.put("content", content);
            newsJSONArray.add(newsJSONObj);

        }

A) 输出有什么问题?B) 当您可以直接使用newsJSONObj中的输出变量时,为什么要调用此escapeHtml3方法?@ErwinBolwidt它是无效的JSON。我使用escaphtml3来编码HTML,但它没有用处,你必须对文本进行JSON(或Javascript)转义,而不是HTML。因此,请使用
StringEscapeUtils.escapeJson(输出)
@Sascha没有“escapeJson”方法啊,该方法是在3.2版中添加的。也许你可以更新库?A)输出有什么问题?B) 当您可以直接使用newsJSONObj中的输出变量时,为什么要调用此escapeHtml3方法?@ErwinBolwidt它是无效的JSON。我使用escaphtml3来编码HTML,但它没有用处,你必须对文本进行JSON(或Javascript)转义,而不是HTML。因此,请使用
StringEscapeUtils.escapeJson(输出)
@Sascha没有“escapeJson”方法啊,该方法是在3.2版中添加的。也许你可以更新库?
   "content":" &lt;p&gt;&lt;img alt=&quot;&quot;   
              src=&quot;data:image\/png;base64,
              iVBORw0KGgoAAAANSUhEUgAAA7YAAAVx