JSON语法和错误

时间:2016-06-23 05:23:37

标签: javascript json

所以这是我目前的代码:

var training = {
"onlineCourses": [
    {
        "name":"Udacity"
        "subjects":["HTML","CSS","JS","Python"]
        "nanodegrees":["Intro to Programming","Front-End Web Developer"]
    }
],
"certifications": [
    {
        "company":"Cisco"
        "name":"Certified Certified Network Associate (CCNA)"
        "subject":"Routing and Networking"
    },
    {
        "company":"CompTIA"
        "name":["A+","Network+"]
        "subject":["General IT","Routing and Networking"]
    }
]
}

在JSONLint中产生此错误:

Error: Parse error on line 3:
..."name": "Udacity"  "subjects": ["HTML",
----------------------^
Expecting 'EOF', '}', ':', ',', ']', got 'STRING'

其次,在我需要这段代码的课程中,他们举例说明我应该写这样的代码:

var training = {
    "onlineCourses": [
        {
            "name":"Udacity"
            "subjects":["HTML","CSS","JS","Python"]
            "nanodegrees":["Intro to Programming","Front-End Web Developer"]
        }
    ]
},
    "certifications": [
        {
            "company":"Cisco"
            "name":"Certified Certified Network Associate (CCNA)"
            "subject":"Routing and Networking"
        },
        {
            "company":"CompTIA"
            "name":["A+","Network+"]
            "subject":["General IT","Routing and Networking"]
        }
    ]

请注意,第一个{在onlineCourses数组之后关闭,而certifications数组不在任何{}集合中。这对我来说似乎不对,我只是认为他们在课堂上输了一个错字。我对此是否正确?

无论哪种方式,我都会得到同样的错误:

Error: Parse error on line 3:
..."name": "Udacity"  "subjects": ["HTML",
----------------------^
Expecting 'EOF', '}', ':', ',', ']', got 'STRING'

5 个答案:

答案 0 :(得分:0)

用逗号(,)

分隔每个键值对
{
    "name":"Udacity",
    "subjects":["HTML","CSS","JS","Python"],
    "nanodegrees":["Intro to Programming","Front-End Web Developer"]
}

答案 1 :(得分:0)

要在对象中指定多个属性,需要使用逗号(,)来分隔每个属性。这是training变量的正确方法。

var training = {
    "onlineCourses": [
        {
            "name":"Udacity",
            "subjects":["HTML","CSS","JS","Python"],
            "nanodegrees":["Intro to Programming","Front-End Web Developer"]
        }
    ],
    "certifications": [
        {
            "company":"Cisco",
            "name":"Certified Certified Network Associate (CCNA)",
            "subject":"Routing and Networking"
        },
        {
            "company":" CompTIA",
            "name": ["A+", "Network+"],
            "subject": ["General IT","Routing and Networking"]
        }
    ]
}

答案 2 :(得分:0)

每个键值对之后应该有逗号(除了集合中的最后一个):

var training = {
  "onlineCourses": [
    {
      "name": "Udacity",
      "subjects": [
        "HTML",
        "CSS",
        "JS",
        "Python"
      ],
      "nanodegrees": [
        "Intro to Programming",
        "Front-End Web Developer"
      ]
    }
  ],
  "certifications": [
    {
      "company": "Cisco",
      "name": "Certified Certified Network Associate (CCNA)",
      "subject": "Routing and Networking"
    },
    {
      "company": "CompTIA",
      "name": [
        "A+",
        "Network+"
      ],
      "subject": [
        "General IT",
        "Routing and Networking"
      ]
    }
  ]
}

答案 3 :(得分:0)

您的JSON无效。您可能始终希望使用http://jsonlint.com/

等验证程序验证JSON

以下是更正后的JSON:

var training = {
    "onlineCourses": [{
        "name": "Udacity",
        "subjects": ["HTML", "CSS", "JS", "Python"],
        "nanodegrees": ["Intro to Programming", "Front-End Web Developer"]
    }],
    "certifications": [{
        "company": "Cisco",
        "name": "Certified Certified Network Associate (CCNA)",
        "subject": "Routing and Networking"
    }, {
        "company": "CompTIA",
        "name": ["A+", "Network+"],
        "subject": ["General IT", "Routing and Networking"]
    }]
}

另外,要回答问题的第二部分,您的JSON看起来也是无效的。但是, 其次,在我需要这段代码的课程中,他们举例说明我应该写这样的代码: isn; t这清楚地总结了格式的任何内容。

答案 4 :(得分:0)

请将json逗号(,)分开以获得有效的语法。 示例

{
   "name":"Udacity",
   "subjects":["HTML","CSS"],
   "nanodegrees":["Intro to Programming","Front-End Web Developer"]
}