JSON 中的正确格式是什么,我还应该引用名称吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3869007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:35:28  来源:igfitidea点击:

What is the correct format in JSON, should I quote names also?

json

提问by Questions

I am writing a JSON file, but I am not sure about which of the following formats is the correct one?

我正在编写一个 JSON 文件,但我不确定以下哪种格式是正确的?

Quoting variable names and all string values

引用变量名和所有字符串值

{
    "class": {
        "number": 2,
        "student": {
            "name": "Tom",
            "age": 1
        },
        "student": {
            "name": "May",
            "age": 2
        }
    }
}

or

或者

Quoting only string values

只引用字符串值

{
    class: {
        number: 2,
        student: {
            name: "Tom",
            age: 1
        },
        student: 
        {
            name: "May",
            age: 2
        }
    }
}  

回答by Nick Craver

The first is valid, if you're unaware you can validate your JSON output online pretty easily here: http://www.jsonlint.com/

第一个是有效的,如果您不知道,您可以在这里很容易地在线验证您的 JSON 输出:http: //www.jsonlint.com/

回答by Greg Hewgill

JSON requires the quotes. See http://json.orgfor the specifications.

JSON 需要引号。有关规范,请参阅http://json.org

In particular, the stringproduction is:

特别是,字符串的产生是:

string
    '"' characters '"'

回答by DaveL17

Old question, but the OP's JSON (first construction) may have the proper syntax, but it's going to cause trouble because it repeats the key student.

老问题,但 OP 的 JSON(第一次构造)可能有正确的语法,但会造成麻烦,因为它重复了 key student

import simplejson

data = '''{
    "class": {
        "number": 2,
        "student": {
            "name": "Tom",
            "age": 1
        },
        "student": {
            "name": "May",
            "age": 2
        }
    }
}'''

data_in = simplejson.loads(data)
print(data_in)

Yields: {'class': {'number': 2, 'student': {'age': 2, 'name': 'May'}}}

产量: {'class': {'number': 2, 'student': {'age': 2, 'name': 'May'}}}

Where unique keys student_1and student_2:

其中唯一键student_1student_2

import simplejson

data = '''{
    "class": {
        "number": 2,
        "student_1": {
            "name": "Tom",
            "age": 1
        },
        "student_2": {
            "name": "May",
            "age": 2
        }
    }
}'''

data_in = simplejson.loads(data)
print(data_in)

Yields: {'class': {'student_1': {'age': 1, 'name': 'Tom'}, 'number': 2, 'student_2': {'age': 2, 'name': 'May'}}}

产量: {'class': {'student_1': {'age': 1, 'name': 'Tom'}, 'number': 2, 'student_2': {'age': 2, 'name': 'May'}}}

UPDATE:
Agree with @Tomas Hesse that an array is better form. It would look like this:

更新:
同意@Tomas Hesse,数组是更好的形式。它看起来像这样:

import simplejson

data = '''{
    "class": {
        "number": 2,
        "students" : [
            { "name" : "Tom", "age" : 1 }, 
            { "name" : "May", "age" : 2 }
        ]
    }
}'''

data_in = simplejson.loads(data)
print(data_in)