Javascript JSON-无效令牌

Javascript JSON-无效令牌,javascript,html,json,Javascript,Html,Json,我第一次看了一段YouTube视频试图学习JSON。这是我们在视频中所做的示例,但在“[我在查看控制台时收到一个无效的令牌错误。对于noob问题,非常抱歉!再次感谢 <html> <head> <title>JSON Example</title> </head> <body> <script type="text/javascript"> let compan

我第一次看了一段YouTube视频试图学习JSON。这是我们在视频中所做的示例,但在“[我在查看控制台时收到一个无效的令牌错误。对于noob问题,非常抱歉!再次感谢

<html>
<head>
    <title>JSON Example</title>
</head>
<body>
    <script type="text/javascript">
        let companies =
        '[
    {
        "name": "Big Corporation",
        "numberOfEmployees": 10000,
        "ceo":  "Mary",
        "rating": 3.6
    },
    {
        "name": "Small Startup",
        "numberOfEmployees": 3,
        "ceo": null,
        "rating": 4.3
    }
]'
    console.log(JSON.parse(companies))
    ((companies)[0].name)
    </script>
</body>
</html>

JSON示例
让公司=
'[
{
“名称”:“大公司”,
“员工人数”:10000,
“首席执行官”:“玛丽”,
“评级”:3.6
},
{
“名称”:“小型初创企业”,
“员工人数”:3,
“ceo”:空,
“评级”:4.3
}
]'
console.log(JSON.parse(公司))
((公司)[0]。名称)

问题不是JSON而是JavaScript。JavaScript中的多行字符串必须使用特定语法

有,但可能最简单的是使用反勾号

<html>
<head>
    <title>JSON Example</title>
</head>
<body>
    <script type="text/javascript">
        let companies =
        `[
    {
        "name": "Big Corporation",
        "numberOfEmployees": 10000,
        "ceo":  "Mary",
        "rating": 3.6
    },
    {
        "name": "Small Startup",
        "numberOfEmployees": 3,
        "ceo": null,
        "rating": 4.3
    }
]`
    console.log(JSON.parse(companies))
    ((companies)[0].name)
    </script>
</body>
</html>

JSON示例
让公司=
`[
{
“名称”:“大公司”,
“员工人数”:10000,
“首席执行官”:“玛丽”,
“评级”:3.6
},
{
“名称”:“小型初创企业”,
“员工人数”:3,
“ceo”:空,
“评级”:4.3
}
]`
console.log(JSON.parse(公司))
((公司)[0]。名称)
顺便说一句,最后一行
((companys)[0].name)
上也会出现错误,因为
console.log
的输出不是一个函数。您可能想再次调用
console.log