Reputation: 91
I have a HTML code that looks like this.
<div>
<script id='jsonData' type="text/x-jquery-tmpl">
[{"legs":"departureAirport":{"airportID":4934980,"airportCity":"Bangkok","airportCode":"DMK","airportName":"Bangkok (DMK)","airportCityState":"Bangkok"}}]
</script>
I want to use JSoup to parse this HTML and get the JSON value in this html. How can I do this?
Upvotes: 1
Views: 6453
Reputation: 11712
You can use this:
String htmlStr = "<div><script id='jsonData' type=\"text/x-jquery-tmpl\">[{\"legs\":\"departureAirport\":{\"airportID\":4934980,\"airportCity\":\"Bangkok\",\"airportCode\":\"DMK\",\"airportName\":\"Bangkok (DMK)\",\"airportCityState\":\"Bangkok\"}}]"
+ "</script></div>";
Document doc = Jsoup.parse(htmlStr);
Element el = doc.getElementById("jsonData");
String jsonStr = el.html();
Basically you get the raw inner html from the script node. Note, that you probably still need to parse the JSON data. There are several libraries available that do that, but JSoup is not one of them.
Upvotes: 2