如果我引用名称,JSON中的格式是什么?

时间:2023-02-07 07:12:12

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
        }
    }
}  

3 个解决方案

#1


19  

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/

#2


10  

JSON requires the quotes. See http://json.org for the specifications.

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

In particular, the string production is:

特别是,字符串制作是:

string

""
" chars "

“”字符“

#3


0  

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(第一个构造)可能有正确的语法,但它会导致麻烦,因为它重复了关键学生。

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_1 and student_2:

唯一键student_1和student_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':'可能'}}}

#1


19  

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/

#2


10  

JSON requires the quotes. See http://json.org for the specifications.

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

In particular, the string production is:

特别是,字符串制作是:

string

""
" chars "

“”字符“

#3


0  

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(第一个构造)可能有正确的语法,但它会导致麻烦,因为它重复了关键学生。

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_1 and student_2:

唯一键student_1和student_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':'可能'}}}