Java Reference
In-Depth Information
Although the data in the JSON response contain the same information as the data in the XML response, the
format is, of course, very different. JSON and XML are both widely used on the Internet, and a large number of web
services offer responses in both formats.
Depending on the use case and the developer, one format might be preferred over the other. In general, JavaFX
applications should be able to work with both formats, because they have to connect with third-party data, and the
JavaFX developer cannot always influence the data format used by the back end.
Many applications allow a number of formats, and by specifying the http “accept” header, the client can
choose between the different formats.
Note
In the next example, we show how to retrieve and parse the JSON response used in the StackExchange Search API.
JSON Response Format
JSON is a very popular format on the Internet, especially in web applications where incoming data are parsed with
JavaScript. JSON data are rather compact and more or less human readable.
A number of tools exist in Java for reading and writing JSON data. As of June 2013, when Java Enterprise Edition 7
was released, there is a standard specification in Java that describes how to read and write JSON data. This Java specification
is defined as JSR 353, and more information can be obtained at http://www.jcp.org/en/jsr/detail?id=353 .
JSR 353 only defines a specification, and an implementation is still needed to do the actual work. In our
examples, we will use jsonp, which is the Reference Implementation of JSR 353. This Reference Implementation can
be found at https://jsonp.java.net/ . Readers are encouraged to try out their favorite implementation of JSR 353,
though.
Although JSR 353 is a specification that is part of the Java Enterprise Edition umbrella, the reference
implementation also works in a Java Standard Edition environment. There are no external dependencies.
We now replace the hard-coded list containing two fake questions with real questions obtained via the StackExchange
REST API. We keep the existing code, but we modify the getObservableList() method as shown in Listing 11-7.
Listing 11-7. Obtain Questions Via the StackExchange REST API, JSON Format and Parse the JSON
ObservableList<Question> getObservableList() throws IOException {
String url = " http://api.stackexchange.com/2.2/search?tagged=javafx&site=stackoverflow ";
URL host = new URL(url);
JsonReader jr = Json.createReader(new GZIPInputStream(host.openConnection().getInputStream()));
JsonObject jsonObject = jr.readObject();
JsonArray jsonArray = jsonObject.getJsonArray("items");
ObservableList<Question> answer = FXCollections.observableArrayList();
jsonArray.iterator().forEachRemaining((JsonValue e) -> {
JsonObject obj = (JsonObject) e;
JsonString name = obj.getJsonObject("owner").getJsonString("display_name");
JsonString quest = obj.getJsonString("title");
JsonNumber jsonNumber = obj.getJsonNumber("creation_date");
Question q = new Question(name.getString(), quest.getString(), jsonNumber.longValue() * 1000);
answer.add(q);
});
return answer;
}
 
 
Search WWH ::




Custom Search