如何从json删除html标签?

时间:2018-06-28 07:37:47

标签: android json

[{"id":1,"header":"<tr><td style=\"width:5.36%\" width=\"5%\" valign=\"top\"></td><td style=\"width:94.48%\" width=\"94%\" colspan=\"2\" valign=\"top\">  <p style=\"margin-bottom:0in;margin-bottom:.0001pt\"><span style='font-family:\"Cambria Math\",serif'>The unit of Wien&#8217;s constant </span><span style='font-size:11.0ptline-height:115%font-family:\"Calibri\",sans-serifposition:relativetop:4.5pt'><img width=\"8\" height=\"20\" src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAUAQMAAACK1e4oAAAAAXNSR0IArs4c6QAAAAZQTFRFAAAAAAAApWe5zwAAAAF0Uk5TAEDm2GYAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAZdEVYdFNvZnR3YXJlAE1pY3Jvc29mdCBPZmZpY2V/7TVxAAAAH0lEQVQYV2NgAAMDBgEg1GMwYlACQjcGF4YKiAQMAAAqagH3u8+wVwAAAABJRU5ErkJggg==\"></span><span style='font-family:\"Cambria Math\",serif'>&nbsp;is </span></p>  </td><td style width=\"0%\" colspan=\"2\"><p>&nbsp;</p></td></tr>","qOptions":"[{\"OptionNo\":\"a\",\"ValueHtml\":\"<tr><td style=\\\"width:5.36%\\\" width=\\\"5%\\\" valign=\\\"top\\\">  <p><span style='font-family:\\\"Cambria\\\",serif'>&nbsp;</span></p>  </td><td style=\\\"width:2.4%\\\" width=\\\"2%\\\">  <p><span style='font-family:\\\"Cambria\\\",serif'>a)</span></p>  </td><td style=\\\"width:92.14%\\\" width=\\\"92%\\\" colspan=\\\"2\\\">  <p style=\\\"margin-bottom:0in;margin-bottom:.0001pt\\\"><span style='font-size:11.0ptline-height:115%font-family:\\\"Calibri\\\",sans-serif'><img width=\\\"65\\\" height=\\\"20\\\" src=\\\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEEAAAAUAQMAAADoesJCAAAAAXNSR0IArs4c6QAAAAZQTFRFAAAAAAAApWe5zwAAAAF0Uk5TAEDm2GYAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAZdEVYdFNvZnR3YXJlAE1pY3Jvc29mdCBPZmZpY2V/7TVxAAAAaUlEQVQYV2NgwAH4GBiYIFJCDAxsDAyPLIAClge4GBicBBjkWwT6TzUwuAowMHAIMsgDWYKbGQSEQDpUFc40yIkwMDMwaCzoaGCQAZkAZgGNgbIEQSwBB6CYQAOI5Xj3AIPlAVwOIU4cAGqKE6T9p+R5AAAAAElFTkSuQmCC\\\"></span></p>  </td><td style width=\\\"0%\\\"><p>&nbsp;</p></td></tr>\"},{\"OptionNo\":\"b\",\"ValueHtml\":\"<tr><td style=\\\"width:5.36%\\\" width=\\\"5%\\\" valign=\\\"top\\\">  <p><span style='font-family:\\\"Cambria\\\",serif'>&nbsp;</span></p>  </td><td style=\\\"width:2.4%\\\" width=\\\"2%\\\">  <p><span style='font-family:\\\"Cambria\\\",serif'>b)</span></p>  </td><td style=\\\"width:92.14%\\\" width=\\\"92%\\\" colspan=\\\"2\\\">  <p style=\\\"margin-bottom:0in;margin-bottom:.0001pt\\\"><span style='font-size:11.0ptline-height:115%font-family:\\\"Calibri\\\",sans-serif'><img width=\\\"51\\\" height=\\\"20\\\" src=\\\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAAAUAQMAAAAaxpfnAAAAAXNSR0IArs4c6QAAAAZQTFRFAAAAAAAApWe5zwAAAAF0Uk5TAEDm2GYAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAZdEVYdFNvZnR3YXJlAE1pY3Jvc29mdCBPZmZpY2V/7TVxAAAAS0lEQVQYV2NgwACMQJEGIGZnYGAG0Yx8BSA++0MW5Q9gPosDkJ9zgJGlAUgbK7AfZWB+wKCkANLAAKKB4hDaAUorAOnyAgagOSQDAOKkDOJJ7pIYAAAAAElFTkSuQmCC\\\"></span></p>  </td><td style width=\\\"0%\\\"><p>&nbsp;</p></td></tr>\"},{\"OptionNo\":\"c\",\"ValueHtml\":\"<tr><td style=\\\"width:5.36%\\\" width=\\\"5%\\\" valign=\\\"top\\\">  <p><span style='font-family:\\\"Cambria\\\",serif'>&nbsp;</span></p>  </td><td style=\\\"width:2.4%\\\" width=\\\"2%\\\">  <p><span style='font-family:\\\"Cambria\\\",serif'>c)</span></p>  </td><td style=\\\"width:92.14%\\\" width=\\\"92%\\\" colspan=\\\"2\\\">  <p style=\\\"margin-bottom:0in;margin-bottom:.0001pt\\\"><span style='font-size:11.0ptline-height:115%font-family:\\\"Calibri\\\",sans-serif'><img width=\\\"32\\\" height=\\\"20\\\" 

我是android的新手,我正在从上述json检索数据并将其显示在列表中。一切正常,但如果我检索到数据,它也会显示html标签。请澄清我的疑问,如何在列表视图中显示时从json中删除html标签。

3 个答案:

答案 0 :(得分:1)

您可以使用正则表达式将其删除-

public class xlWatermark {
    public static void main(String[] args) {
        HSSFWorkbook wb = new HSSFWorkbook();
        FileOutputStream fileOut = null;
        try {
            fileOut = new FileOutputStream("Test.xls");
            HSSFSheet ws = wb.createSheet("testSheet");
            HSSFPatriarch dp = ws.createDrawingPatriarch();
            HSSFClientAnchor anchor = new HSSFClientAnchor
                (0, 0, 1023, 255, (short) 2, 4, (short) 13, 26);
            HSSFTextbox txtbox = dp.createTextbox(anchor);
            HSSFRichTextString rtxt = new HSSFRichTextString("test");
            HSSFFont font = wb.createFont();
            font.setColor((short) 27);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
            font.setFontHeightInPoints((short) 192);
            font.setFontName("Verdana");
            rtxt.applyFont(font);
            txtbox.setString(rtxt);
            txtbox.setLineStyle(HSSFShape.LINESTYLE_NONE);
            txtbox.setNoFill(true);
            wb.write(fileOut);
            fileOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }

完整的代码示例为-

var myString = '[{"id":1,"header":"<tr><td style=\"width:5.36%\" width=\"5%\">Test</td>"}]';
var newString = myString.replace(/<(?:.|\n)*?>/gm, '');

希望这对您有所帮助。

答案 1 :(得分:0)

myHtmlString.replaceAll(“ s /<(.*?)>// g”,“”); Html.fromHtml(htmlSrc).toString();

答案 2 :(得分:0)

这是一种不好的方法...您必须通过json validate来验证json响应。

尝试更改来自后端的响应... 甚至您也可以通过在Java中将字符串替换为null来替换[“,;,/ ... etc] ...

  • html = html.replaceAll(“ <​​(。?)\>”,“”); //删除其中的所有项目 括号       html = html.replaceAll(“ <​​(。?)\\ n”,“”); //必须在下面       html = html.replaceFirst(“(。*?)\>”,“”); //将所有连接的项目移至最后一个括号       html = html.replaceAll(“”,“”);       html = html.replaceAll(“&”,“”);