Reading JSON Content

Viewed 28740

I'm using jsoup to scrape some HTML data and it's working out great. Now I need to pull some JSON content (only JSON, not HTML). Can I do this easily with jsoup or do I have to do it using another method? The parsing that jsoup performs is encoding the JSON data so it's not parsing properly with Gson.

6 Answers

An old question but struggled a bit to figure this out. Jsoup can fetch the JSON data if you set ignoreContentType to true. However, it still wraps the JSON content in HTML tags like this.

<html>
 <head></head>
 <body>
{ JSON DATA }
 </body>
</html>

In order to remove this, we can simply get the body content as given below.

Connection connection = Jsoup.connect("URL").ignoreContentType(true);       
connection.execute();

String strJsonData = connection.get().body().text();
Related