1

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.