简体   繁体   中英

Cannot retrieve a specific value from a json object

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?

You can use array syntax on javascript Objects. So that would look like oddsData.events[0].odds["3W"] .

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

You should use

oddsData.events[0].odds["3W"].home

You could have written it like oddsData.events[0].odds.3W.home but 3W is not a valid property name (names cannot start with number) hence it is put in the square brackets

Also to know complete set of naming rules please read it at MDN https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Values,_variables,_and_literals

It can be accessed using the [] syntax. Property names that start with a number can't be accessed with the . syntax.

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

Rule of thumb is to access object or array keys using [] syntax if those keys do not satisfy variable naming rules or matching reserved javascript keywords such as "var", "each", "length" etc.

Even if javascript won't break on this it still can produce logic or runtime errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM