Java Reference
In-Depth Information
var locationURL = bind
"http://local.yahooapis.com/"
"LocalSearchService/V3/localSearch?"
"appid=YahooDemo&query=city&zip= {zipCode} &"
"results=1&output=json";
Being bound to the zipCode field, which is entered by the user, the string is
updated automatically with its value when changed. This request returns location
data for the ZIP code, as well as local search results that are limited to one result
as specified in the URL. The only data we're interested in, however, are the fields
of the Location class, shown in Listing 10.7.
Listing 10.7
The Location Data Structure
class Location {
public var city: String;
public var state: String;
public var lat: String;
public var long: String;
}
These fields are updated when the JSON names that match are located within the
JSON text, as read by the PullParser class, locationParser (see Listing 10.8).
Although many other fields and values are encountered while parsing the loca-
tion data, only the four Location class values are stored.
Listing 10.8
The Location JSON Data Parser
var locationInput: InputStream;
var locationParser = PullParser {
documentType: PullParser.JSON;
input: bind locationInput
onEvent: function(event: Event) {
// parse the JSON Yahoo data and
//populate the location object
if(event.type == PullParser.END_VALUE) {
//println("{event}");
if(event.name == "City") {
location.city = event.text;
}else if (event.name == "Latitude") {
location.lat = event.text;
}else if (event.name == "Longitude") {
location.long = event.text;
}else if (event.name == "State") {
location.state = event.text;
}
 
Search WWH ::




Custom Search