09.04.2016 Views

www.ebook777.com

Make_Getting_Started_with_Processing_Second_Edition

Make_Getting_Started_with_Processing_Second_Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

174 Getting Started with Processing<br />

<strong>www</strong>.<strong>ebook777.com</strong><br />

}<br />

float rating = film.getFloat("rating");<br />

println(title + " by " + dir + ", " + year);<br />

println("Rating: " + rating);<br />

The JSONObject class is used to create a code object to store the<br />

data. Once that data is loaded, each individual piece of data can<br />

be read in sequence or by requesting the data related to each<br />

label. Notice that different data types are requested by the<br />

name of the data type. The getString() method is used for the<br />

name of the film and the getInt() method is used for the<br />

release year.<br />

Example 12-5: Visualize Data from a<br />

JSON File<br />

To work with the JSON file that includes more than one film, we<br />

need to introduce a new class, the JSONArray. Here, the data file<br />

started in the previous example has been updated to include all<br />

of the director’s films from 1960–1966. The name of each film is<br />

placed in order on screen according to the release year and<br />

assigned a gray value based on the rating value.<br />

There are several differences between this example and Example<br />

12-4 on page 173. The most important is the way the JSON<br />

file is loaded into Film objects. The JSON file is loaded within<br />

setup() and each JSONObject that represents a single film is<br />

passed into the constructor of the Film class. The constructor<br />

assigns the JSONObject data to String, float, and int fields<br />

inside each Film object. The Film class also has a method to<br />

display the name of the film:<br />

Film[] films;<br />

void setup() {<br />

size(480, 120);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!