Pergunta

The below string is what I retrieve from one of the fields of json response >How do I get the value of src in the string below .I really appreciate any help.Thanks in Advance.

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>
Foi útil?

Solução

Try using jsoup html parsing api to with dedicated functionality for html parsing and would also provide for an extensible solution.

For your case (I escape quotes and additional \ to make it a valid Java string):

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

Output;

http://vthumb.ak.fbcdn.net/hvthumb-ak-ash3/t15/1095964_10151882078663445_10151882076318445_40450_2013_b.jpg
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top