Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSoup: Requesting JSON response

Tags:

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 it because JSoup don't know how to handle JSON ? (offcourse there is no )
  • Or because there is an error in my request ?

Is there JSoup like libraries for JSON ?

like image 654
Jean-Philippe Encausse Avatar asked Aug 20 '11 16:08

Jean-Philippe Encausse


People also ask

Can Jsoup parse JSON?

While great, Jsoup is a HTML parser, not a JSON parser, so it is useless in this context. If you ever attempt it, Jsoup will put the returned JSON implicitly in a <html><head> and so on. You don't want to have that. Gson is a JSON parser, so you definitely need it.


1 Answers

You can fetch JSON or other data format using this:

// JSON example String json = Jsoup.connect(url).ignoreContentType(true).execute().body(); 
like image 156
Anchor Avatar answered Feb 06 '23 12:02

Anchor