简体   繁体   English

无法从json对象检索特定值

[英]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 : 基本上,我有这个json对象,示例值为:

{
    "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. 我认为我必须使用oddsData.events[0].odds.3W但这不起作用,并且oddsData.events[0].odds.home打印出未定义的内容。 I'm stuck. 我被卡住了。 Any ideas? 有任何想法吗?

You can use array syntax on javascript Objects. 您可以在javascript对象上使用数组语法。 So that would look like oddsData.events[0].odds["3W"] . 这样看起来就像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. 3W不是有效的标识符,因为它以数字开头,因此您不能使用点符号来访问它。 You will be able to access the object using: 您将能够使用以下方法访问该对象:

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

Valid identifers must start with a unicode letter, $ , \\ or _ . 有效的标识符必须以unicode字母$\\_开头。 For more information see http://www.ecma-international.org/ecma-262/5.1/#sec-7.6 有关更多信息,请参见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 您可能像oddsData.events[0].odds.3W.home这样写,但3W不是有效的属性名称(名称不能以数字开头),因此将其放在方括号中

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 另外,要了解完整的命名规则集,请在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. 经验法则是使用[]语法访问对象或数组键,如果这些键不满足变量命名规则或不匹配保留的javascript关键字,例如“ var”,“ each”,“ length”等。

Even if javascript won't break on this it still can produce logic or runtime errors. 即使javascript不会中断,它仍然会产生逻辑或运行时错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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