从JSON数据获取特定属性

时间:2018-12-11 17:01:42

标签: javascript arrays json postman

我的api调用具有此结果JSON数据,但是当我无法访问属性“ 69106658_5”中的数据时,我无法获得“错误:未捕获的SyntaxError:无效或意外的令牌”。我在下面的在线编辑器上有我正在运行的副本。我猜是因为该属性包含下划线。

let results=
    {
    "links": {
        "data": {
            "self": {
                "body": "",
                "content_type": "",
                "href": "/api/v2/nodes/69107289/categories",
                "method": "GET",
                "name": ""
            }
        }
    },
    "results": [
        {
            "data": {
                "categories": {
                    "58652374_10": [
                        "16",
                        "16.0.1",
                        "16.2",
                        "16.2.4"
                    ],
                    "58652374_11": [
                        "English"
                    ],
                    "58652374_12": [
                        "Windows"
                    ],
                    "58652374_13": "2018-11-20T00:00:00",
                    "58652374_2": "Published",
                    "58652374_3": "19",
                    "58652374_4": "Video",
                    "58652374_5": "65",
                    "58652374_6": "How To",
                    "58652374_7": [
                        "basic"
                    ],
                    "58652374_8": "237",
                    "58652374_9": "Content Server"
                }
            }
        },
        {
            "data": {
                "categories": {
                    "69106658_2": "You Tube",
                    "69106658_3": [
                        "End User"
                    ],
                    "69106658_4": [
                        "69106508:7"
                    ],
                    "69106658_5": "https://img.youtube.com/vi/j-aOeCpRvEs/hqdefault.jpg",
                    "69106658_6": false,
                    "69106658_7": "Engineering",
                    "69106658_8": null
                }
            }
        }
    ]
    }

    var lookInto = results.results;

    for( let key in lookInto ) {
     var selectData = lookInto[key].data.categories;
     console.log(selectData);
    }

    console.log( selectData.69106658_5 )

2 个答案:

答案 0 :(得分:5)

以字母(和_等符号)开头的属性字段,必须使用方括号表示法。

尝试使用selectData.69106658_5代替selectData['69106658_5']

答案 1 :(得分:1)

下划线不应引起任何问题。 如果要访问属性“ 69106658_5”,则应这样做:

results.results [1] .data.categories [“ 69106658_5”]