【问题标题】:parse html from string in android json response从android json响应中的字符串解析html
【发布时间】:2014-04-12 09:44:41
【问题描述】:

以下字符串是我从 json 响应的字段之一中检索到的内容

Getting better doesn’t stop because it’s getting colder. The best athletes don’t just overcome the elements, they embrace them with Nike Hyperwarm. Gear up for winter: <a href="/l.php?u=http%3A%2F%2Fwww.nike.com%2Fhyperwarm&amp;h=4AQEBIq17&amp;s=1" rel="nofollow nofollow" target="_blank" onmouseover="LinkshimAsyncLink.swap(this, &quot;http:\/\/www.nike.com\/hyperwarm&quot;);" onclick="LinkshimAsyncLink.swap(this, &quot;\/l.php?u=http\u00253A\u00252F\u00252Fwww.nike.com\u00252Fhyperwarm&amp;h=4AQEBIq17&amp;s=1&quot;);">http://www.nike.com/hyperwarm</a><br/><br/><a href="http://www.facebook.com/photo.php?v=10151882076318445" id="" title="" target="" onclick="" style=""><img class="img" src="http://vthumb.ak.fbcdn.net/hvthumb-ak-ash3/t15/1095964_10151882078663445_10151882076318445_40450_2013_b.jpg" alt="" style="height:90px;" /></a><br/><a href="http://www.facebook.com/photo.php?v=10151882076318445" id="" style="">Winning in a Winter Wonderland</a>

【问题讨论】:

    标签: android html-parsing


    【解决方案1】:

    尝试使用jsoup html parsing api 来为html 解析提供专用功能,并且还将提供可扩展的解决方案。

    对于您的情况(我转义引号和附加 \ 以使其成为有效的 Java 字符串):

    String str = "Getting better doesn’t stop because it’s getting colder. The best athletes don’t just overcome the elements, they embrace them with Nike Hyperwarm. Gear up for winter: <a href=\"/l.php?u=http%3A%2F%2Fwww.nike.com%2Fhyperwarm&amp;h=4AQEBIq17&amp;s=1\" rel=\"nofollow nofollow\" target=\"_blank\" onmouseover=\"LinkshimAsyncLink.swap(this, &quot;http:\\/\\/www.nike.com\\/hyperwarm&quot;);\" onclick=\"LinkshimAsyncLink.swap(this, &quot;\\/l.php?u=http\u00253A\u00252F\u00252Fwww.nike.com\u00252Fhyperwarm&amp;h=4AQEBIq17&amp;s=1&quot;);\">http://www.nike.com/hyperwarm</a><br/><br/><a href=\"http://www.facebook.com/photo.php?v=10151882076318445\" id=\"\" title=\"\" target=\"\" onclick=\"\" style=\"\"><img class=\"img\" src=\"http://vthumb.ak.fbcdn.net/hvthumb-ak-ash3/t15/1095964_10151882078663445_10151882076318445_40450_2013_b.jpg\" alt=\"\" style=\"height:90px;\" /></a><br/><a href=\"http://www.facebook.com/photo.php?v=10151882076318445\" id=\"\" style=\"\">Winning in a Winter Wonderland</a>\"";
    Document doc = Jsoup.parse(str);
    Element element = doc.select("img").first();
    System.out.println(element.attr("src"));
    Element element2 = doc.select("a").first(); // Get the anchor tag element
    System.out.println(element2.attr("onclick")); // onclick as attribute for anchor tag
    

    输出;

    http://vthumb.ak.fbcdn.net/hvthumb-ak-ash3/t15/1095964_10151882078663445_10151882076318445_40450_2013_b.jpg
    

    【讨论】:

    • 如何I escape quotes and additional \ .PLease 让我知道?谢谢
    • 另外我如何获得onclick值?
    • @jason 我这样做是为了演示目的,尝试将您的 json 响应字符串提供给 Jsoup,它会负责解析。对于 onclick,请参阅编辑
    • 我实际上想要在 onclick 中使用正确链接的价值。我得到\/l.php?u=http\u00253A\u00252F\u00252Fwww.nike.com\u00252Fhyperwarm&amp;amp;h=4AQEBIq17&amp;amp;s=1&amp;quot;) 这是将其重定向到原始链接的链接。我想要那个链接。我如何获得它?没有u00253A 和其余部分的清除链接。这来自 facebook 提要。
    • 非常感谢任何帮助。谢谢
    猜你喜欢
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多