String
to the constructor of the {@link
JsonFse.JsonObj} class. Example: String s = "{" +
"\"name\" : \"Mike\"," +
"\"age\" : 23," +
"\"hobbies\" : [ \"fishing\", \"rugby\", \"computer games\" ]" +
"\"wife\"
: " +
"{
"
+
"\"name\" : \"Eve\"," +
"\"age\" :
21" +
"} " +
"}";
try
{
JsonObj
jo = new JsonObj(s);
System.out.println(jo.getAsString());
}
catch (ParseErrorEx ex)
{
System.out.println(ex.getErrMsg());
}
JsonObj.main()
. JsonObj
tree some knowledge of
the node types is necessary. Their names reflect the names given in
www.json.org. The abstract base
class is {@link JsonFse.Node}, {@link JsonFse.NodePair} represents a
pair, {@link JsonFse.NodeNumber} a number etc. To get pairs in a
JSON object there are the methods {@link JsonFse.JsonObj#getNext}
and {@link JsonFse.JsonObj#get}. The same methods are available in
the {@link JsonFse.NodeArray} class. Example traversing through the
JSON object jo
created above: NodeString ns, nsa;
NodeValue nv;
NodePair np =
jo.getNext();
while (np !=
null)
{
ns
= np.getString();
System.out.println("label: " + ns.getAsString());
nv = np.getValue();
if (nv instanceof NodeArray)
{
nsa = (NodeString)nv.get(1);
System.out.println("Second
hobby of Mike: " + nsa.getAsString());
}
np = jo.getNext();
}
System.out.println("First pair in jo: " +
jo.get(0).getAsString());
label: "name"
label: "age"
label: "hobbies"
Second hobby of Mike: "rugby"
label: "wife"
First pair in jo: "name" : "Mike"
instanceof
to detect the type of unknown values as shown above.NodeLabel
, its name is {@link
JsonFse.NodeString} instead, because of the wording in www.json.org.It is possible to build up a JSON object by adding single pairs
to it. The following code creates the same object as shown above:
// Create an
empty JSON object:
JsonObj mo = new
JsonObj();
// Create an empty
wife sub JSON object:
JsonObj wo = new
JsonObj();
// Create the hobby
NodeArray:
NodeArray na = new
NodeArray();
na.add(new
NodeString("fishing"));
na.add(new
NodeString("rugby"));
na.add(new
NodeString("computer games"));
// Add the pairs to
the wife JSON object:
wo.addPair("name", "Eve");
wo.addPair("age", 21);
// Putting all
togeteter and add the pairs to the Mike JSON object:
mo.addPair("name",
"Mike");
mo.addPair("age", 23);
mo.addPair("hobbies", na);
mo.addPair("wife", wo);
// Check the parsed tree:
System.out.println("A JSON
object constructed step by step:\n" + mo.getAsString());
When creating NodeString
s or adding pairs
containing strings it is not necessary to add the extra quotes
around the strings; this is done automatically.
Already parsed JSON objects can be reused by providing the
constructor of a new JsonObj
with the old root node:
// Make a copy of Eve's
sub JSON object and add her profession:
NodeObj no =
(NodeObj)mo.getValue("wife");
JsonObj eo = new
JsonObj(no);
eo.addPair("profession", "sales representative");
System.out.println("Mike's wife:\n" + eo.getAsString());
In the above example the new JsonObj
eo
is
created by initializing it with the wife's NodeObj
. Of
course it can modified then e. g. by adding a pair.
object = '{' , [ members ] , '}'
;
members = pair , { ',' , pair }
;
pair = string , ':' ,
value
;
string = '"' , { char } , '"' ;
char = ? any Unicode character ?
- ctrlChar - '\') | ctrlChar | hexNumber
;
ctrlChar = '\"' | '\\' | '\/' | '\b' | '\f' |
'\n' | '\r' | '\t'
;
hexNumber = '\u' , hexDigit , hexDigit , hexDigit ,
hexDigit
;
hexDigit = digit09 | hexChar
;
digit09 = digit0 | digit19
;
digit0 = '0'
;
digit19 = '1' | '2' | '3' | '4' | '5' | '6'
| '7' | '8' | '9'
;
hexChar = 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
| 'a' | 'b' | 'c' | 'd' | 'e' | 'f'
;
value = string | number |
object | array | 'true' | 'false' | 'null'
;
number = int | int frac | int exp |
int , frac , exp
;
int = [ '-' ] ,
digit | digit19 , digits
;
frac = '.' , digits
;
exp = eE , digits
digits = digit09 , { digit09 }
;
eE = 'e' | 'E' [
'+' | '-' ]
;
array = '[' , [ elements ] ,
']'
;
elements = value , { ',' , value }
;
Symbol | Meaning |
---|---|
= | definition |
, | concatenation |
; | termination |
| | alternation |
[ ... ] | option |
{ ... } | repetition |
' ... ' | terminal string |
? ... ? | special sequence |
- | exception |
16 December 2018, V.1.1.0:
JsonObj(NodeObj)
and new method JsonObj.setNodeObj(NodeObj)
are available; an already parsed JSON object can be reused
without parsing its string again.NodePair(
String, int)
and
new method JsonObj.addPair(String, int)
to handle
integer values.