【发布时间】:2018-03-17 04:23:58
【问题描述】:
假设我有三个模型和表格,即学校、班级和学生。 学校和班级是一对多的关系,班级和学生也是一对多的关系。现在我正在创建一个 RESTFUL API 以这种 JSON 格式一次发送所有这些数据:
[{
"1": {
"school_id": 1,
"school_name": "Havard",
"Class": {
"1": {
"class_id": 1,
"school_id": 1,
"class": "6",
"Student": {
"1": {
"Student_fname": "SomeName",
"Student_lname": "Last Name"
},
"2": {
"Student_fname": "Another",
"Student_lname": "AnotherLast"
}
},
"2": {
"class_id": 2,
"school_id": 28,
"class": "7",
"Student": {
"1": {
"Student_fname": "Anotherone",
"Student_lname": "Last"
},
"2": {
"Student_fname": "New",
"Student_lname": "Newer"
}
}
}
}
}
}
},
{
"2": {
"school_id": 1,
"school_name": "AnotherSchool",
"Class": {
"1": {
"class_id": 1,
"school_id": 1,
"class": "6",
"Student": {
"1": {
"Student_fname": "SomeName",
"Student_lname": "Last Name"
},
"2": {
"Student_fname": "Another",
"Student_lname": "AnotherLast"
}
},
"2": {
"class_id": 2,
"school_id": 28,
"class": "7",
"Student": {
"1": {
"Student_fname": "Anotherone",
"Student_lname": "Last"
},
"2": {
"Student_fname": "New",
"Student_lname": "Newer"
}
}
}
}
}
}
}
]
如何实现这种格式?我的这个 json 格式正确吗?有没有更好的方法来实现它?
【问题讨论】:
-
我认为类(如果类不止一个)和学生应该是对象数组(json)。
-
是的,一所学校的课程不止一个。你能举一个代码的例子来说明清楚吗?