Java Reference
In-Depth Information
Note that because some fields differ in their value types (i.e.,
String
,
Number
,
and
Integer
), Java classes such as
Double
and
Integer
are used to get the
proper values from the JSON text.
Listing 10.10
The Weather JSON Data Parser
var weatherInput: InputStream;
var weatherParser = PullParser {
documentType: PullParser.JSON;
input: bind weatherInput
onEvent: function(event: Event) {
// Parse the JSON Weather data and
// populate the Weather object
if(event.type == PullParser.END_VALUE) {
if(event.name == "clouds") {
weather.clouds = event.text;
}else if (event.name == "stationName") {
weather.station = event.text;
}else if (event.name == "windDirection") {
weather.windDirection = event.integerValue;
}else if (event.name == "windSpeed") {
weather.windSpeed = Double.valueOf(event.text);
}else if (event.name == "temperature") {
weather.temperature = Double.valueOf(event.text);
}else if (event.name == "dewPoint") {
weather.dewPoint = Double.valueOf(event.text);
}else if (event.name == "humidity") {
weather.humidity = event.integerValue;
}else if (event.name == "seaLevelPressure") {
weather.seaLevelPressure = event.numberValue;
} else if (event.name == "observation") {
weather.observation = event.text;
}
}
}
}
As with the request to Yahoo! for the location data, since
weatherParser
's
member variable,
input
, is bound to the
weatherInput
variable, this code is
invoked when the input stream is updated and
parse
is called. This is triggered
when the location data is completely received. When all of the weather data is
received, and the
Weather
class' member variables have been set, the JavaFX
Text
components that are bound to them are automatically updated. This completes
the weather widget's request, update, and display processes. When compared to
implementing the same functionality with plain Java code, the power of JavaFX
object binding greatly simplifies the entire process.
Search WWH ::
Custom Search