Jean-Philippe Encausse
Jean-Philippe Encausse

Reputation: 1521

JSoup: Requesting JSON response

I'm using JSoup to authenticate then connect to a website. Some URL have a JSON response (because part of the site is in AJAX). Can JSoup handle JSON response ?

Connection.Response doc = Jsoup.connect("...")
                               .data(...)
                               .cookie(...)
                               .header(...)
                               .method(Method.POST)
                               .execute();
String result = doc.body()

In my case body is "".

Is there JSoup like libraries for JSON ?

Upvotes: 28

Views: 38763

Answers (4)

Danyial Shahid Ali
Danyial Shahid Ali

Reputation: 511

Try Like This

Use Header "Accept:text/javascript"

 String InboxJson=Jsoup.connect("https://www.fiverr.com/conversations/Json")
                            .timeout(1000000) 
                            .header("Accept", "text/javascript")
                            .userAgent("Mozilla/5.0 (Windows NT 6.1; rv:40.0) Gecko/20100101 Firefox/40.0")
                            .get()
                            .body()
                            .text();

Upvotes: 7

Anchor
Anchor

Reputation: 621

You can fetch JSON or other data format using this:

// JSON example
String json = Jsoup.connect(url).ignoreContentType(true).execute().body();

Upvotes: 62

Zarathustra
Zarathustra

Reputation: 2943

You should use a JSON library to process JSON Data.

Here are some: Click

Upvotes: 0

scott
scott

Reputation: 974

I don't think Jsoup will execute Javascript. If the provided URL returns any non-html text, I believe Jsoup will just wrap it in a body tag or something similiar.

See this post for a suggestion

Upvotes: 0

Related Questions