0

我正在尝试创建一个 JSON 对象文件,但我不确定如何记录所有可用滑冰课程的列表。目前我有以下内容:

[
{"classname":"FS1","classdays":["monday", "wednesday"]},
{"classname":"FS2","classdays":  ["tuesday", "thursday"]}
]

它有效,唯一的问题是每个班级“classname”,每天都有多个班级时间“classdays”。而且我不确定如何添加另一个时间数组并将它们与特定类的特定日期相关联,并将其添加到我当前的 JSON 文件中以创建该结构。

以下是非编程语言的类 - 为清楚起见:

Classname: FS1
Monday classes are 12:00PM and 2:00PM
Wednesday classes are 1:00PM and 3:00PM
Classname: FS2
Tuesday classes are 4:00PM and 6:00PM
Thursday classes are 2:00PM and 8:00PM
Classname: FS3
Monday classes are 2:00PM and 4:00PM
Thursday classes are 2:00PM and 8:00PM

我愿意接受有关如何最好地构建或在需要时进行重组的建议。

4

3 回答 3

1

在对象中创建每个条目classdays,以日期名称和时间作为键:

[
    {
        "classname": "FS1",
        "classdays": [
            {
                "day": "monday",
                "times": [
                    "12:00",
                    "14:00"
                ]
            },
            {
                "day": "tuesday",
                "times": [
                    "16:00",
                    "18:00"
                ]
            },
            ...more days here...
        ]
    },
    ...more classses here...
]

可以创建classdays一个对象而不是数组,以日期名称为键:

[
    {
        "classname": "FS1",
        "classdays": {
            "monday": [
                "12:00",
                "14:00"
            ],
            "tuesday": [
                "16:00",
                "18:00"
            ],
            ...more days here...
        }
    },
    ...more classes here...
]

......但是日子没有秩序。当然,我希望您的代码逻辑可以知道星期几的顺序,但我想我可能会使用第一个结构。

于 2012-11-17T07:14:37.113 回答
1

首先,只是指出 JSON 不是一种“编程语言”,它是一种数据语言。不同之处在于 JSON,就像 XML不做任何事情,它只是描述对象。

其次,我认为您前进的方向会让您直接意识到 JSON 不是关系型的。

最后,这是我的做法:

[
    { "classname":"FS1",
      "classdays":[
        { "classday":"Monday",
          "classtimes": ["12:00 PM", "2:00 PM"]
        }, 
        { "classday":"Wednesday",
          "classtimes": ["1:00 PM", "3:00 PM"]
        }
        ]
    },
    { "classname":"FS2",
      "classdays":[
        { "classday":"Tuesday",
          "classtimes": ["4:00 PM", "6:00 PM"]
        }, 
        { "classday":"Thursday",
          "classtimes": ["2:00 PM", "8:00 PM"]
        }
        ]
    },
    ... And so on.
]
于 2012-11-17T07:15:53.197 回答
0
{"classname":"FS1","classdays":[{"day":"monday","times":["12:00PM","2:00PM"]},{"day":"wednesday" ,"次":["1:00PM","3:00PM"]}]}
于 2012-11-17T07:26:06.067 回答