JSoup: Requesting JSON response

2020-06-30 05:26发布

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 ?

标签: java json jsoup
4条回答
兄弟一词,经得起流年.
2楼-- · 2020-06-30 05:44

You can fetch JSON or other data format using this:

// JSON example
String json = Jsoup.connect(url).ignoreContentType(true).execute().body();
查看更多
做个烂人
3楼-- · 2020-06-30 05:53

You should use a JSON library to process JSON Data.

Here are some: Click

查看更多
▲ chillily
4楼-- · 2020-06-30 05:56

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();
查看更多
Rolldiameter
5楼-- · 2020-06-30 05:59

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

查看更多
登录 后发表回答