Before we can extract the earthquake data that we need for our app, we should
discuss the structure of format of the data that we get back from the USGS API.
When we say format,
we mean to discuss the specifics of how the data is organize or structure, so
that we know how to dissect and extract the parts we need for an app.
Earlier in this lesson, we reviewed some of the HTTP request parameters that
were available to us to query the USGS dataset.
One of those parameters format equals value allows us to specify
the response format or structure of how we'd like the response data to be sent.
We opted for format=geojson,
which sends the data back to us in a structure using the JSON syntax rules.
While there are other formats that have strengths and weaknesses and
histories beyond the scope of this class we chose JSON because this is
the most common response format used by many of today's signature websters.
That's not to say JSON has decidedly better than any other formats such
as XML.
Moreover, we encourage you to explore other formats in your free time
to get a better sense of the design intention, the strengths and
weaknesses, and even the histories of those formats.
Like all technology the web has changed rapidly over time in
various formats have emerged.
Rest assured though, the experience you will gain by familiarizing yourself with
JSON will also help you pick up on other formats quickly.
So, let's zoom in on JSON.
For the USGS earthquake API, we have a format called geojson available.
Geojson is just a special flavor of JSON,
custom-tailored to represent geographical information.
JSON stands for JavaScript, Object, Notation.
For the name you might assume that is tightly coupled with a JavaScript
programming language, that's actually not the case.
JSON well originally designed to help facilitate efficient communication on
the web and hence it's title JavaScript
is really just a set of some tactical rules to organize data and
thus is completely programming language independent.
So, JSON can be interpreted using any programming language of choice,
including the Java code that we use to build our Android apps.
Let's start to learn how to read JSON by looking at a really simple example.
This example describes a kind of shoe.
Let's start at the root left brace which notes the start of a JSON object.
Everything inside it before the closing right brace is its contents.
You can think of these braces as the container of staff.
At the heart of the JSON's syntactical structures as the notion of
key value pairing.
Syntactically, the key is on the left of the colon and the value is on the right.
This pairing is simply a way of encapsulating a tie or an association
between a name of our choosing, a.k.a., the key, and its corresponding value.
It turns out this is an important semantic in programming and
computer science.
Why?
Well, this mapping or binding of a key to a value
helps us recall the data layer in an easier and more logical manner.
In other words, when we want to pull up or recall the data layer
instead of having to remember all the specifics of the data because in some
case it could be a lot of data, we can do this just by using its tag or key.
Think of a coat check when you're going to the symphony hall.
You might have dropped off a lot of items at the coat check counter, but
all you need to do to get your stuff back is to hold on to your ticket and
give it to the attendant at the end of the show.
Let's look at a few examples of keys.
Here we have the string key size, which references a positive decimal number,
which happens to be for the shoe size of 9.5.
When we see a comma, this is simply a delimiter to let us know that there's
another key value pair following.
So we also have wide, which maps to the boolean value of true.
Then we have country of origin, which is a string set to USA.
This key value is semantic and starting to feel pretty straightforward.
Cool!
These have been pretty simple keys, keys with logical names that map to simple
primitives that you've been used to.
Numbers, booleans, and strings.
But wait, the key style maps to a value of what,
what is this brace here, there's a whole bunch of other stuff going on.
What are you doing to me here?
This could be kind of interesting, and in fact, this is where the power and
extensibility of JSON as a data format lies.
Let's take a break, and
we'll take a deeper look at the possible values in the next node.