Hardware Reference
In-Depth Information
The response will likely be together on one line, but if you were to add line
breaks and indentation, it would look like Example 6-4 . There are three key/
value pairs: the number of hours until the next issue, the next volume number,
and the number of days until the next issue.
Example 6-4. JSON response
{
totalHours : 1473 ,
volumeNumber : "38" ,
daysAway : 61
}
The code in Example 6-5 uses the Python programming language to connect
to the server's JSON feed at http://nextmakemagazine.appspot.com/json
and parses the volume number and number of hours.
Example 6-5. Using Python to parse JSON
import json #
import urllib2 #
httpResponse = urllib2 . urlopen ( 'http://nextmakemagazine.appspot.com/json' ) #
jsonString = httpResponse . read () #
jsonData = json . loads ( jsonString ) #
print "Volume" , jsonData [ 'volumeNumber' ], "will be released in" , \
jsonData [ 'totalHours' ], "hours." #
Import Python's JSON library in order to parse the JSON response.
Import Python's urllib2 library to fetch the data from the server.
Connect to the server to request the JSON feed and store the
response in httpResponse .
Store the body of the response in jsonString .
Convert the string into a Python data object.
Print the information out.
There are a few different ways to put Python code onto the board. In this
section, you'll connect to the board and enter the code into the text editor,
vi.
In order to use Python and save your files on the board, you
must boot Galileo with an SD card. See Appendix D for more
information on how to create a bootable MicroSD card.
 
Search WWH ::




Custom Search