Java Reference
In-Depth Information
Create the
JSONObject
from the input.
or
g.json.JSONArray
doesn't implement
Iterable
so you can't use a foreach loop.
Running it does produce the expected output:
Software Name: robinparse
Version: 1.2.3
Description: Another Parser for JSON
Class: RobinParse
Contributor Name: Robin Smythe
Contributor Name: Jon Jenz
Contributor Name: Jan Ardann
JSONObject
and
JSONArray
use their
toString()
method to produce (correctly format-
ted) JSON strings. For example:
public
public class
class
WriteOrgJson
WriteOrgJson
{
public
public static
void
main
(
String
[]
args
) {
JSONObject jsonObject
=
new
static
void
new
JSONObject
();
jsonObject
.
put
(
"Name"
,
"robinParse"
).
put
(
"Version"
,
"1.2.3"
).
put
(
"Class"
,
"RobinParse"
);
String printable
=
jsonObject
.
toString
();
System
.
out
.
println
(
printable
);
}
}
Ni
ce that it offers a “fluent API” to allow chaining of method calls
to
String()
converts to textual JSON representation.
Running this produces the following:
{
"Name"
"Name"
:
"robinParse"
,
"Class"
"Class"
:
"RobinParse"
,
"Version"
"Version"
:
"1.2.3"
}