Unable to retrieve a specific value from a json object

advertisements

This has been bothering me for at least 2 hours now. Basically, I've got this json object and the example values are :

{
    "events": [
        {
            "id": 64714,
            "live": false,
            "start": "1399117500",
            "league_code": "SOCENGPRE",
            "home_id": "30866",
            "away_id": "30860",
            "home_name": "West Ham",
            "away_name": "Tottenham",
            "odds": {
                "3W": {
                    "home": "4.15",
                    "away": "1.88",
                    "draw": "3.60"
                }
            },
        },
        {
            "id": 64712,
            "live": false,
            "start": "1399125600",
            "league_code": "SOCENGPRE",
            "home_id": "30792",
            "away_id": "30856",
            "home_name": "Stoke",
            "away_name": "Fulham",
            "odds": {
                "3W": {
                    "home": "2.32",
                    "away": "3.10",
                    "draw": "3.35"
                }
            },
        },...

This line of code :

prettyprintJSON(oddsData.events[0].odds);

Which refers to :

function prettyprintJSON (jsondata) {
    // prints a human readable form of JSON
    pretty = JSON.stringify(jsondata, null, 4);
    $("#resultsbox").html("<pre>"+pretty+"</pre>")
}

Prints out :

{
    "3W": {
        "home": "4.15",
        "away": "1.88",
        "draw": "3.60"
    }
}

But now I'm stuck. I want to retrieve the home/away/draw values but I can't. I'd think I'd have to use oddsData.events[0].odds.3W but that doesn't work and oddsData.events[0].odds.home prints out undefined. I'm stuck. Any ideas?


3W is not a valid identifier as it starts with a digit, so you can not access it using the dot notation. You will be able to access the object using:

oddsData.events[0].odds['3W']

Valid identifers must start with a unicode letter, $, \ or _. For more information see http://www.ecma-international.org/ecma-262/5.1/#sec-7.6