Java Reference
In-Depth Information
The first thing we need to do is create a new instance of java.io.StringReader
from our JSON string. We do this by passing a string containing our JSON data to the
constructor of StringReader . Then, we pass the resulting StringReader instance to
the static createReader() method of JSON-P's javax.json.Json class. This method
invocation returns an implementation of the javax.json.JsonReader interface,
which we then use to obtain an implementation of javax.json.JsonObject .
The JSON object contains several get methods we can use to obtain data from the
JSON string. These methods take the JSON property name as a parameter and return
the corresponding value. In our example we used two of these methods, namely
getString() and getInt() , and we used them to populate an instance of our
Person object. The following table summarizes all of the available get methods:
get method
Description
getBoolean(String
name)
This returns the value of the specified property as a Boolean.
getInt(String name)
This returns the value of the specified property as an integer.
getJsonArray(String
name)
This returns the value of the specified property as an array in
the form of a JsonArray implementation.
getJsonNumber(String
name)
This returns the value of the specified numeric property
as a JsonNumber implementation. The value can then
be converted to int , long , or double by invoking
intValue() , longValue() , or doubleValue() ,
respectively.
getJsonObject(String
name)
This returns the value of the specified property as a
JsonObject implementation.
getJsonString(String
name)
This returns the value of the specified property as a
JsonString implementation.
getString(String
name)
This returns the value of the specified property as a string.
Let's go back to our example. You can see that the parseJson() method of our
JsonModelApiBean controller class returns the string display_populated_obj .
Therefore, based on JSF conventions, we know that it will navigate to a JSF page
named display_populated_obj.xhtml . The markup for this page is as follows:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Java Object Properties Populated from JSON</title>
Search WWH ::




Custom Search