OpenWeatherMap syntax error

I'm using the OpenWeatherMap API and I'm trying to get the rain value. According to the documentation I can get there by data.list[0].rain.3h. But the .3h is giving me a syntax error. I'm new to this so my question is how can I get the value without the error.

Tagged:

Answers

  • Property identifiers must adhere to proper naming conventions - this includes not starting with a number. Property names however can be pretty much any string. It's a perhaps confusing distinction; but all you need to know is that if a property name doesn't conform to the accepted identifier syntax you must access it via bracket notation:

    data.list[0].rain["3h"];

    The same applies to properties with spaces in the name: foo["My property Name"];

  • Thanks, just what I needed

  • hmm, no more error but I get a undefined message now. The path doesn't seem right now.

  • Use the console to check the structure of your data using: console.log(data);

    Chrome will output the structure in a navigable format...

    From this documentation I'd expect the rain property to be a direct child of data; but I guess it might be accessible via other API calls...

  • I found the problem. When there's no rain in the forecast it's not in the JSON and thus the path is wrong. Thanks for the help.

Sign In or Register to comment.