1

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>

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

6 Comments

How do I escape quotes and additional \ .PLease let me know?Thanks
Also how do I get onclick value ?
@jason I did it for demo purpose itself, try feeding your json response string to Jsoup and it would take care of parsing. For onclick, see the edit
I actually want value within onclick with corrent link .I am getting \/l.php?u=http\u00253A\u00252F\u00252Fwww.nike.com\u00252Fhyperwarm&amp;h=4AQEBIq17&amp;s=1&quot;) This is the link to redirect it to original link.I want that link .How do I get it ? CLean link without u00253A and the rest.This from facebook feed.
When I print to console I get" LinkshimAsyncLink.swap(this, "\/l.php?u=http%3A%2F%2Fwww.nike.com%2Fhyperwarm&h=4AQEBIq17&s=1");
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.