我刚刚学习JSON& Javascript对象并试图找出为特定项目构建对象的最佳方法。
我认为我已经很好地绘制了这个对象,但是我已经想到了两种不同的方法,我不确定哪种方式更好“,而且这种做法的含义是什么呢?另一个。
{
"session": {
"sessionId": "",
"sessionTitle": "",
"sessionDescription": "",
"clientName": "",
"sessionTopics": [{
"topicNames": [
"name of first topic",
"name of second topic",
"name of third topic",
"name of fourth topic",
"name of fifth topic"
],
"topicChoices": [
["a", "c", "a", "b", "e", "a", "d", "e", "c", "b"],
["c", "b", "a", "c", "d", "a", "b", "c", "a", "b"],
["a", "b", "a", "b", "e", "a", "d", "e", "c", "d"],
["a", "c", "a", "b", "e", "a", "d", "e", "c", "b"],
["c", "d", "b", "c", "e", "b", "d", "e", "c", "a"]
],
"topicLmode": [
7, 9, 6, 9, 5
]
}],
"imageDescriptions": [
"client's description of image A",
"client's description of image B",
"client's description of image C",
"client's description of image D",
"client's description of image E"
]
}
}
- 或 -
{
"session": {
"sessionId": "",
"sessionTitle": "",
"sessionDescription": "",
"clientName": "",
"sessionTopics": [{
"firstTopic": [{
"topicName": "name of first topic",
"topicChoices": [
"b", "c", "a", "d", "e", "a", "e", "b", "c", "b"
],
"lmodeRating" : 7
}]
}, {
"secondTopic": [{
"topicName": "second topic name",
"topicChoices": [
"a", "c", "a", "b", "e", "a", "d", "e", "c", "b"
],
"lmodeRating": 9
}]
}, {
"thirdTopic": [{
"topicName": "third topic name",
"topicChoices": [
"a", "c", "a", "b", "e", "a", "d", "e", "c", "b"
],
"lmodeRating": 6
}]
}, {
"fourthTopic": [{
"topicName": "fourth topic name",
"topicChoices": [
"a", "c", "a", "b", "e", "a", "d", "e", "c", "b"
],
"lmodeRating": 9
}]
}, {
"fifthTopic": [{
"topicName": "fifth topic name",
"topicChoices": [
"a", "c", "a", "b", "e", "a", "d", "e", "c", "b"
],
"lmodeRating": 5
}]
}],
"imageDescriptions": [
{
"imageA": "description of image A"
}, {
"imageB": "description of image B"
}, {
"imageC": "description of image C"
}, {
"imageD": "description of image D"
}, {
"imageE": "description of image E"
}
],
}
}
我不知道它会对哪个更好有所不同,但我不会提前知道每个会话中会有多少“主题”(我知道它可能总是4个) -6个主题)。
也可能相关 - 我需要能够按主题获取某些计算和输出的数据,以及跨主题的计算(例如,比较两个主题的'topicChoices' - 第四选择相同的主题1与主题4一样?)。
我的直觉告诉我,或许这使得第一种方法更好(更容易使用),但我不确定。
我愿意接受建议!
由于 斯科特
答案 0 :(得分:2)
我更喜欢第二个,因为在这种情况下,您可以轻松找出各个主题的名称,选项和lmodeRating,因为它被组合在一起,更易于阅读/理解和维护。
但是我也会稍微修改一下。像这样:
"topic": "secondTopic",
"details": {
"topicName": "second topic name",
"topicChoices": [
"a", "c", "a", "b", "e", "a", "d", "e", "c", "b"
],
"lmodeRating": 9
}
请注意,我已将单个主题详细信息从数组更改为对象。
现在,无需数组索引即可轻松访问所有详细信息。
例如。 session.sessionTopics[0].details.topicName
代替session.sessionTopics[0].firstTopic[0].topicName
我总是喜欢事先知道我的钥匙。
答案 1 :(得分:1)
我建议使用第3版,它允许更好地迭代sessionTopics
var data = {
"session": {
"sessionId": "",
"sessionTitle": "",
"sessionDescription": "",
"clientName": "",
"sessionTopics": [
{
"topicName": "name of first topic",
"topicChoices": [
"b", "c", "a", "d", "e", "a", "e", "b", "c", "b"
],
"lmodeRating": 7
}, {
"topicName": "second topic name",
"topicChoices": [
"a", "c", "a", "b", "e", "a", "d", "e", "c", "b"
],
"lmodeRating": 9
}
],
"imageDescriptions": [
{
"imageA": "description of image A"
}, {
"imageB": "description of image B"
}, {
"imageC": "description of image C"
}, {
"imageD": "description of image D"
}, {
"imageE": "description of image E"
}
],
}
};