1

I am using the Wunderground api to get hourly weather forecast for an android app. I want to receive information like Temp and day.

An example of the Json is:

"hourly_forecast": [
    {
    "FCTTIME": {
    "hour": "11","hour_padded": "11","min": "00","min_unpadded": "0","sec": "0","year": "2014","mon": "7","mon_padded": "07","mon_abbrev": "Jul","mday": "8","mday_padded": "08","yday": "188","isdst": "1","epoch": "1404842400","pretty": "11:00 AM PDT on July 08, 2014","civil": "11:00 AM","month_name": "July","month_name_abbrev": "Jul","weekday_name": "Tuesday","weekday_name_night": "Tuesday Night","weekday_name_abbrev": "Tue","weekday_name_unlang": "Tuesday","weekday_name_night_unlang": "Tuesday Night","ampm": "AM","tz": "","age": "","UTCDATE": ""
    },
    "temp": {"english": "61", "metric": "16"},
    "dewpoint": {"english": "56", "metric": "13"},
    "condition": "Partly Cloudy",
    "icon": "partlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
    "fctcode": "2",
    "sky": "58",
    "wspd": {"english": "7", "metric": "11"},
    "wdir": {"dir": "SW", "degrees": "232"},
    "wx": "Partly Cloudy",
    "uvi": "9",
    "humidity": "83",
    "windchill": {"english": "-9999", "metric": "-9999"},
    "heatindex": {"english": "-9999", "metric": "-9999"},
    "feelslike": {"english": "61", "metric": "16"},
    "qpf": {"english": "0", "metric": "0"},
    "snow": {"english": "0", "metric": "0"},
    "pop": "2",
    "mslp": {"english": "29.92", "metric": "1013"}
    }

It seems to be in an array but when i tried the following it came up with a error of not being a json object.

String row = rootArray.getAsJsonObject().get("sky").getAsString(); 

** Edit ** I was able to get it to work with the code

String skyText = rootobj.getAsJsonObject().getAsJsonArray("hourly_forecast").get(0).getAsJsonObject().get("sky").getAsString(); 

with rootobj being my json object

steveS
  • 13
  • 7

1 Answers1

0

The JSON you posted is not an array. Instead, it is an object with a key named "hourly_forecast" and that key points to a value that is an array.

Key                Value
hourly_forecast    JSONArray

So if your JSON was in an string called wundergroundData, you'd access the "sky" value like this:

JSONObject json = new JSONObject(wundergroundData);
String skyText = json.getJSONArray("hourly_forecast").get(0).getString("sky");

or, to more closely follow the example you posted, I imagine something like this would work as well:

String row = rootArray.getAsJsonObject().getJSONArray("hourly_forecast").get(0).getString("sky"); 

Although, you'd probably want to rename the rootArray variable because the root is actually a JSONObject. Not an array.

If this doesn't work, post your FULL JSON and I can tell you exactly what you need to do to work with it. Right now, you're missing either a curly brace or a square brace at the start, as well as the ending of the JSON. I'm assuming here that your JSON starts with a curly brace. If it starts with a square brace then you will need something like the following code instead:

String row = rootArray.get(0).getJSONArray("hourly_forecast").get(0).getString("sky"); 
gMale
  • 17,147
  • 17
  • 91
  • 116
  • Thanks a lot for your help. It seemed to be a little bit off but it sent me in the right direction. The code i used is String skyText = rootobj.getAsJsonObject().getAsJsonArray("hourly_forecast").get(0).getAsJsonObject().get("sky").getAsString(); – steveS Jul 09 '14 at 04:05
  • Glad that helped. One tip, if you're going to do a lot of JSON work, it's important to verify that objects aren't null and arrays aren't empty as you try to access them. For that reason, alone, at work we tend to use [GSON](https://code.google.com/p/google-gson/) for our heavy-duty JSON work so that we can work with model objects, directly, which reduces the number of null checks we have to do, in many cases. It also makes sense because we use those models everywhere in our app, anyway. – gMale Jul 09 '14 at 19:42