Tuesday, 3 September 2013

Can't get Value from JSON object using a valid Key

Can't get Value from JSON object using a valid Key

I have a JSON array that stores information about a number of locations.
From this array I am trying to extract the street name, street number, zip
code of an address. The JSON array is provided by a php webservice that
outputs a JSON array of all the locations I need. I use an
HttpURLConnection to send a GET request and read in the contents of the
page with a buffered reader into a string. I then use that string to
create the JSON array like so:
JSONArray arr = new JSONArray(s);
I can successfully pull the "name" field out of each JSON Object. However
when trying to get the street name for each location by looping over the
array like this:
arr.getJSONObject(i).getString("street_name")
It is throwing a JSON Exception. I stepped through with eclipse's debugger
and found that in the JSON array the correct JSON String was represented:
{"street_number":xxx,"zipcode":"xxxxx","street_name":"xxxxx","name":"xxxxxx"}
but when I looked at the table for each of the JSON Objects in the array,
only street number and zipcode were present. Name and street name were
absent. Curiously enough,
arr.getJSONObject(i).getString("name")
works just fine.
Here is a screenshot of the debugger that may be helpful.
As you can see at the very bottom, all the information is present.
Can anybody see why I cannot get the street name out of this JSON array?
Any help will be appreciated.

No comments:

Post a Comment