3

I have the following JSON Data:

{
    "city": {
        "city_code":"DE0001516",
        "post":"28195",
        "forecast": {
            "2012-09-10": {
                "p":"24",
                "w":"10",
                "06:00": {
                    "p":"5",
                    "w":"20",
                    "tn":"15",
                    "tx":"21",
                    "w_txt":"wolkig"
                }
            }
        }
    }
}

Normally I read the data through this function:

function(data){ $("#").html(data.city.post); }

How do I get the data from 06:00?

function(data){ $("#").html( data.city.forecast.2012-09-10.06:00.w); }

doesn’t work. I think this has something to do with time and date format.

Pent Ploompuu
  • 5,316
  • 1
  • 31
  • 46
Daria Duda
  • 33
  • 4
  • It has to do with how you notate keys on JavaScript objects. Try this: `data.city.forecast["2012-09-10"]["06:00"].w` – Will Sep 13 '12 at 20:02

4 Answers4

2

You can't read property names with those special characters in them. They need to be quoted:

data.city.forecast["2012-09-10"]["06:00"].w

Here's a related question:

any string can be a property name ... some properties can only be accessed using the bracket syntax.

Community
  • 1
  • 1
jbabey
  • 44,525
  • 12
  • 67
  • 94
  • Thank you! Every day will change the json file and the date will be overwritten with a current date. How should I query? If I write in my javascript for exemple 2012-09-10, my javascript will not be able to read the data on the next day. – Daria Duda Sep 13 '12 at 20:17
  • @DariaDuda make them dynamic e.g. `var date = "2012-09-10"; data.city.forecast[date];` – jbabey Sep 13 '12 at 21:26
1

Try with :

data.city.forecast['2012-09-10']['06:00'].w

Another problem is $("#"). The selector seems to be wrong. Which element are you targeting ?

ChristopheCVB
  • 7,181
  • 1
  • 28
  • 54
1

Use bracket notation:

data.city.forecast['2012-09-10']['06:00']
moonwave99
  • 20,750
  • 2
  • 41
  • 64
0

Try this

data.city.forecast["2012-09-10"]["06:00"].w
Sushanth --
  • 54,565
  • 8
  • 62
  • 98