Java Reference
In-Depth Information
public
public static
void
main
(
String
[]
args
) {
LocalDate dNow
=
LocalDate
.
now
();
System
.
out
.
println
(
toJson
(
dNow
));
static
void
}
public
public static
static
String
toJson
(
LocalDate dNow
) {
StringBuilder sb
=
new
new
StringBuilder
();
sb
.
append
(
OPEN
).
append
(
"\n"
);
sb
.
append
(
jsonize
(
"year"
,
dNow
.
getYear
()));
sb
.
append
(
jsonize
(
"month"
,
dNow
.
getMonth
()));
sb
.
append
(
jsonize
(
"day"
,
dNow
.
getDayOfMonth
()));
sb
.
append
(
CLOSE
).
append
(
"\n"
);
return
return
sb
.
toString
();
}
public
public static
static
String
jsonize
(
String key
,
Object value
) {
return
return
String
.
format
(
"\"%s\": \"%s\",\n"
,
key
,
value
);
}
}
Of course, this is an extremely trivial example. For anything more involved, or for the com-
mon case of having to parse JSON objects, using one of the frameworks will be easier on
your nerves.
Parsing and Writing JSON with Jackson
Problem
You want to read and/or write JSON using a full-function JSON API.
Solution
Use Jackson, the full-blown JSON API.