Reading JSON Content

Jsoup is not designed for parsing JSON. Use Gson(or any other java JSON library). For getting remote content with Jsoup use this:

Connection  con = HttpConnection.connect(url);
con.method(Method.POST).data(data.params).ignoreContentType(true);
Response resp = con.execute();
String body = resp.body();

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.

Your concrete problem is likely that you don't know how to feed an URL returning a JSON to Gson. In that case, you need to use URL#openStream() to get an InputStream of it and use InputStreamReader to decorate it into a Reader which finally can be fed to Gson#fromJson() which accepts a Reader.

InputStream input = new URL("http://example.com/foo.json").openStream();
Reader reader = new InputStreamReader(input, "UTF-8");
Data data = new Gson().fromJson(reader, Data.class);
// ...

Tags:

Java

Gson

Jsoup