在不使用列表的情况下创建JSON对象

时间:2015-01-19 16:49:11

标签: json lint

我希望能够创建一个JSON对象,以便我可以像这样访问它。

education.schools.UNCC.graduation

目前,我的JSON是这样的:

var education  = {
    "schools": [
        "UNCC": {
            "graduation": 2015, 
            "city": "Charlotte, NC", 
            "major": ["CS", "Spanish"]
        },
        "UNC-CH": {
            "graduation": 2012,
            "city": "Chapel Hill, NC"
            "major": ["Sociology", "Film"]
        }
    ],
    "online": {
        "website": "Udacity",
        "courses": ["python", "java", "data science"]
    }
};

当我转到Lint my JSON时,我收到一条错误消息。 我知道我可以重新格式化我的对象来访问它(如下所示),但我不想这样做。我希望能够拨打学校名称,而不是使用索引号。

education.schools[1].graduation

2 个答案:

答案 0 :(得分:0)

对象具有命名键。数组是成员列表。

用对象替换"schools"的值。将[]更改为{}

答案 1 :(得分:-1)

这是你的JSON更正。 您的JSON无效。

{
    "schools": [
        {
            "UNCC": {
                "graduation": "2015",
                "city": [
                    "CS",
                    "Spanish"
                ],
                "major": [
                    "CS",
                    "Spanish"
                ]
            }
        },
        {
            "UNC-CH": {
                "graduation": "2012",
                "city": [
                    "Chapel Hill",
                    "NC"
                ],
                "major": [
                    "Sociology",
                    "Film"
                ]
            }
        }
    ],
    "online": {
        "website": "Udacity",
        "courses": [
            "python",
            "java",
            "data science"
        ]
    }
}

<强>解释

  1. &#34; city&#34;:&#34; Chapel Hill,NC&#34; - &GT;这是一个包含2个值的数组&#34; Chapel Hill &#34;和&#34; HC &#34;,就像你对专业和课程一样。
  2. Schools数组,你需要使用这个sintaxe来构造一个数组[{
  3. http://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html