JSON双引号之谜

2024-09-27 23:25:13 发布

您现在位置:Python中文网/ 问答频道 /正文

学习JSON,我想应该是遵循了语法技巧。我想开始将数据集加载到JSON中并使用Python操作它们。在写数据集时,我开始输入数据,但一直出现这个错误。你知道吗

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 7 column 2 (char 129)

虽然我遵循了多个教程,我不能理解我的错误是从哪里来的?你知道吗

        [
            {
                "name": "x",
                "email": "x@x",
                "location": "Yorkville Village",
                "contacted": "Yes",
            },
            {
                "name": "y",
                "email": "y@y",
                "location": "Yorkville Village",
                "contacted": "Yes",
            },
            {
                "name": "z",
                "email": "info@z.com",
                "location": "Yorkville Village",
                "contacted": "Yes",
            },
        ]

Tags: 数据namejson技巧email错误语法location
2条回答

它需要“用双引号括起来的属性名”,如“需要用双引号括起来的[另一个]属性名;它需要另一个属性。这是因为在k:v对的末尾有额外的逗号。你知道吗

你想要:

        [
            {
                "name": "x",
                "email": "x@x",
                "location": "Yorkville Village",
                "contacted": "Yes"
            },
            {
                "name": "y",
                "email": "y@y",
                "location": "Yorkville Village",
                "contacted": "Yes"
            },
            {
                "name": "z",
                "email": "info@z.com",
                "location": "Yorkville Village",
                "contacted": "Yes"
            }
        ]

问题中显示的Json无效,因为每个对象中都有最后一个逗号。你知道吗

以下是有效版本:

[
    {
        "name": "x",
        "email": "x@x",
        "location": "Yorkville Village",
        "contacted": "Yes"
    },
    {
        "name": "y",
        "email": "y@y",
        "location": "Yorkville Village",
        "contacted": "Yes"
    },
    {
        "name": "z",
        "email": "info@z.com",
        "location": "Yorkville Village",
        "contacted": "Yes"
    }
]

始终使用JSON validator检查JSON是否有效

相关问题 更多 >

    热门问题