Parts of Wunderground API Inaccessible In Swift

87 Views Asked by At

I am using the Wunderground API in my project, and the part of the API I want to use looks like this:

"history": {
    "dailysummary": [
    { "date": {
    "pretty": "12:00 PM PDT on August 12, 2015",
    "year": "2015",
    "mon": "08",
    "mday": "12",
    "hour": "12",
    "min": "00",
    "tzname": "America/Los_Angeles"
    },
    "fog":"0","rain":"0","snow":"0","snowfallm":"0.00", "snowfalli":"0.00","monthtodatesnowfallm":"", "monthtodatesnowfalli":"","since1julsnowfallm":"", "since1julsnowfalli":"","snowdepthm":"", "snowdepthi":"","hail":"0","thunder":"0","tornado":"0","meantempm":"26", "meantempi":"79","meandewptm":"16", "meandewpti":"60","meanpressurem":"1014", "meanpressurei":"29.94","meanwindspdm":"9", "meanwindspdi":"5","meanwdire":"","meanwdird":"331","meanvism":"16", "meanvisi":"10","humidity":"","maxtempm":"33", "maxtempi":"91","mintempm":"19", "mintempi":"66","maxhumidity":"78","minhumidity":"34","maxdewptm":"17", "maxdewpti":"62","mindewptm":"15", "mindewpti":"59","maxpressurem":"1016", "maxpressurei":"30.01","minpressurem":"1012", "minpressurei":"29.88","maxwspdm":"24", "maxwspdi":"15","minwspdm":"0", "minwspdi":"0","maxvism":"16", "maxvisi":"10","minvism":"16", "minvisi":"10","gdegreedays":"28","heatingdegreedays":"0","coolingdegreedays":"14","precipm":"0.00", "precipi":"0.00","precipsource":"","heatingdegreedaysnormal":"0","monthtodateheatingdegreedays":"0","monthtodateheatingdegreedaysnormal":"0","since1sepheatingdegreedays":"","since1sepheatingdegreedaysnormal":"","since1julheatingdegreedays":"0","since1julheatingdegreedaysnormal":"17","coolingdegreedaysnormal":"5","monthtodatecoolingdegreedays":"106","monthtodatecoolingdegreedaysnormal":"69","since1sepcoolingdegreedays":"","since1sepcoolingdegreedaysnormal":"","since1jancoolingdegreedays":"600","since1jancoolingdegreedaysnormal":"280" }
    ]
}

For some reason, dailysummary, which has both "{}" brackets and "[]" brackets, cannot be accessed the way I would normally, like this:

var jsonData = json["history"]["dailysummary"]["fog"] 

which, if this worked normally, would return the fog value in my function. The function works fine; I've tested it with other parts of the API. Is there something specific that needs to be done for dailysummary?

1

There are 1 best solutions below

0
On BEST ANSWER

You're misinterpreting how to call json. It might help to beautify the json to look like the below:

{
    "history": {
        "dailysummary": [{
            "date": {
                "pretty": "12:00 PM PDT on August 12, 2015",
                "year": "2015",
                "mon": "08",
                "mday": "12",
                "hour": "12",
                "min": "00",
                "tzname": "America/Los_Angeles"
            },
            "fog": "0",
            ...
        }]
    }
}

With the above it's pretty self explanatory that dailysummary is an array containing a single object. To access 'fog' you call it via json.history.dailysummary[0].fog