乐闻世界logo
搜索文章和话题

Do the JSON keys have to be surrounded by quotes?

1个答案

1

According to the JSON (JavaScript Object Notation) specification, it is a lightweight data interchange format that is easy for humans to read and write, and also easy for machines to parse and generate. In JSON, object keys must be strings and must be enclosed in double quotes (" "). This differs slightly from how object keys are handled in JavaScript. In JavaScript code, if a key is a valid identifier (such as one that does not contain special characters and is not a JavaScript reserved word), the key can be written without quotes. However, in JSON format, keys must always be enclosed in double quotes. For example, the following is a JSON object that conforms to the standard:

json
{ "name": "Zhang San", "age": 30, "isStudent": false }

If the double quotes around the keys are removed, as shown below:

json
{ name: "Zhang San", age: 30, isStudent: false }

This would not be a valid JSON format, and attempting to parse it with a JSON parser would result in an error. Therefore, when writing or processing JSON data, ensure that keys are enclosed in double quotes.

2024年6月29日 12:07 回复

你的答案