简体   繁体   English

如何解决我的 JSON object 中的这个语法错误?

[英]How can I solve this syntax error in my JSON object?

First and foremost, I'd like to address that JSON is fairly new to me so expect my code to look faulty.首先,我想说明 JSON 对我来说相当新,所以希望我的代码看起来有问题。 I have a JSON object that defines two clubs: "Oak Club" and "Bravo Club" with its members.我有一个 JSON object 定义了两个俱乐部:“橡树俱乐部”和“布拉沃俱乐部”及其成员。 Note, that this code was written in Python.请注意,此代码是用 Python 编写的。

Here is the JSON object:这是 JSON object:

json_clubs = {
    "clubs": [
        {
            "Oak Club": {
                    "members": [
                        "Charles",
                        "Nora",
                        "Tom",
                        "Paul"
                        ]
                    }
            }
    ],
    [
        {
            "Bravo Club": {
                "members": [
                    "Nathaniel",
                    "Roselyn",
                    "Ash"
                    ]
                }
            }
        ]
    }

This code raises the syntax error: ':' expected after dictionary key.此代码引发语法错误:字典键后应出现“:”。

VSCode highlights the "clubs" key-value pair and states: End of file expected. VSCode 突出显示“clubs”键值对并声明:预期文件结束。

After inspecting my code, I can't seem to figure out where I went wrong since all my keys have colons separating keys from their values.检查我的代码后,我似乎无法弄清楚我哪里出错了,因为我所有的键都有冒号将键与它们的值分开。

Rather than including the two club objects inside of the clubs array, you only adding the Oak Club object to it and then defining another array to the JSON object that is anonymous.您只需将Oak Club object 添加到它,然后将另一个数组定义到 JSON object 中,而不是将两个俱乐部对象包含在clubs数组中。 Here's a better way to structure that code:这是构建该代码的更好方法:

json_clubs = {
    "clubs": [
        {
            "Oak Club": {
                "members": [
                    "Charles",
                    "Nora",
                    "Tom",
                    "Paul"
                ]
            }
        },
        {
            "Bravo Club": {
                "members": [
                    "Nathaniel",
                    "Roselyn",
                    "Ash"
                ]
            }
        }
    ]
}

You are trying to map clubs to two separate arrays (each containing a single object), rather than a single array with two objects.您正在尝试将 map clubs分成两个单独的 arrays(每个都包含一个对象),而不是一个包含两个对象的单个数组。

json_clubs = {
    "clubs": [
        {
            "Oak Club": {
                    "members": [
                        "Charles",
                        "Nora",
                        "Tom",
                        "Paul"
                        ]
                    }
        },
        {
            "Bravo Club": {
                "members": [
                    "Nathaniel",
                    "Roselyn",
                    "Ash"
                    ]
                }
        }
    ]
}

Line 13 you're closing the array instead of the object.第 13 行您将关闭阵列而不是 object。

json_clubs = {
  "clubs": [
    {
      "Oak Club": {
        "members": [
          "Charles",
          "Nora",
          "Tom",
          "Paul"
        ]
      },
      "Bravo Club": {
        "members": [
          "Nathaniel",
          "Roselyn",
          "Ash"
        ]
      }
    }
  ]
}

In the current format, you are missing a key and colon for the second item.在当前格式中,您缺少第二项的键和冒号。 WIthout changing it, you would fix by entering its key here:在不更改它的情况下,您可以通过在此处输入其密钥来修复:

json_clubs = {
    "clubs": [
        {
            "Oak Club": {
                    "members": [
                        "Charles",
                        "Nora",
                        "Tom",
                        "Paul"
                        ]
                    }
            }
    ],
    "KEY NAME GOES HERE" : [
        {
            "Bravo Club": {
                "members": [
                    "Nathaniel",
                    "Roselyn",
                    "Ash"
                    ]
                }
            }
        ]
    }

However, based on the item names, it appears that maybe something like this would be the more appropriate solution.但是,根据项目名称,似乎这样的解决方案可能是更合适的解决方案。

json_clubs = {
  "clubs": [
    {
      "Oak Club": {
        "members": [
          "Charles",
          "Nora",
          "Tom",
          "Paul"
        ]
      },
      "Bravo Club": {
        "members": [
          "Nathaniel",
          "Roselyn",
          "Ash"
        ]
      }
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM