Chapter 14. Processing JSON Data
14.0 Introduction
JSON, or JavaScript Object Notation, is all of the following:
-
A simple, lightweight data interchange format.
-
A simpler, lighter alternative to XML.
-
Easy to generate with
println()
or with one of several APIs. -
Recognized directly by the JavaScript parser in all web browsers.
-
Supported with add-on frameworks for all common languages (Java, C/C++, Perl, Ruby, Python, Lua, Erlang, Haskell, to name a few); a ridiculously long list of supported languages (including two dozen parsers for Java alone) is right on the home page.
A simple JSON message might look like this:
json/src/main/resources/json/softwareinfo.json/
{
"name"
:
"robinparse"
,
"version"
:
"1.2.3"
,
"description"
:
"Another Parser for JSON"
,
"className"
:
"RobinParse"
,
"contributors"
:
[
"Robin Smythe"
,
"Jon Jenz"
,
"Jan Ardann"
]
}
As you can see, the syntax is simple, nestable, and amenable to human inspection.
The JSON home page provides a concise summary of JSON syntax.
There are two kinds of structure: JSON objects (maps) and JSON arrays (lists).
JSON objects are sets of name and value pairs, which can be represented
either as a java.util.Map
or as the properties of
a Java object. For example, the fields of a LocalDate
(see Recipe 6.1) object
for April 1, 2019, might be represented like this:
{
"year"
:
2019
,
"month"
:
4
,
"day"
:
1
}
JSON arrays are ordered lists, represented in Java either as arrays or as java.util.List
s. A list of two dates might look ...
Get Java Cookbook, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.