Java Reference
In-Depth Information
jackson.org parser
Widely used because it's very powerful, and used with Spring Framework
javax.json
Oracle's new official but currently EE-only standard
This chapter shows several ways of processing JSON data using some of the various APIs
just listed. Disturbingly, the “official” javax.json API is only included in the Java EE, not
the Java SE, so it is unlikely to see very much use on the client side. This API uses some
names in common with the org.json API, but not enough to be consdered “compatible.”
Because this is a book for client-side Java developers, nothing will be made of the ability to
process JSON directly in server-generated browser-based JavaScript, though this can be very
useful in building enterprise applications.
Generating JSON Directly
Problem
You want to generate JSON without bothering to use an API.
Solution
Get the data you want, and use println() or String.format() as appropriate.
Discussion
There is nothing the APIs do that you can't do yourself. For the utterly trivial cases, you can
just use the PrintWriter.println() or String.format() .
This code prints the year, month, and date from a LocalTime object (see Finding Today's
Date ) . Some of the JSON formatting is delegated to the toJson() object:
public
public class
class LocalDateToJsonManually
LocalDateToJsonManually {
private
private static
static final
final String OPEN = "{" ;
private
private static
static final
final String CLOSE = "}" ;
Search WWH ::




Custom Search