Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading JSON Content

Tags:

java

gson

jsoup

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.

like image 306
Chromag Avatar asked Jun 14 '11 19:06

Chromag


People also ask

How do I decode a JSON file?

You just have to use json_decode() function to convert JSON objects to the appropriate PHP data type. Example: By default the json_decode() function returns an object. You can optionally specify a second parameter that accepts a boolean value. When it is set as “true”, JSON objects are decoded into associative arrays.

How do I read a JSON message in Python?

Instead of the JSON loads method, which reads JSON strings, the method used to read JSON data in files is load(). The load() method takes up a file object and returns the JSON data parsed into a Python object. To get the file object from a file path, Python's open() function can be used.


1 Answers

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);
// ...
like image 133
BalusC Avatar answered Sep 27 '22 20:09

BalusC