Consider this scenario:
考虑一下这个场景:
You want to send some data to the client in JSON format, and you don't want to go back to the server. The data consists of 15 teachers with 100 students. The relationship between these entities is many to many (each student learn many teachers and each teacher teaches to many students).
您希望以JSON格式向客户端发送一些数据,而不希望返回到服务器。数据由15名教师和100名学生组成。这些实体之间的关系很多(每个学生学习许多老师,每个老师教许多学生)。
In client, user is presented with the list of students. On click of any student, the list of his/her teachers would be presented to the user, and on click of a teacher, the list of all students of that teacher would be presented. This results in infinite click-through style navigation from students to teachers and vice verca.
在客户端,用户显示学生列表。点击任何一个学生,他/她的老师名单就会呈现给用户,点击一个老师,就会显示该老师所有学生的名单。这导致了从学生到教师和副verca的无限的点击样式导航。
Now, as you know, JSON only represents one-to-many relationship in this form:
现在,正如您所知道的,JSON仅表示这种形式的一对多关系:
{ "s1" : [ "t1", "t2"], "s2" : [ "t2", "t4" ], "s3" : [ "t1", "t3", "t4"], ...}
Do you have any idea on how to do this?
你知道怎么做吗?
2 个解决方案
#1
12
As JSON does not have a concept of references, you should not need to worry about them. That which defines what counts as a relation between teachers and students lies outside of the data, i.e. is simply a matter of your interpretation during runtime, through the entities' identifiers.
由于JSON没有引用的概念,所以不需要担心它们。定义什么是老师和学生之间的关系不在数据之内,也就是说仅仅是你在运行时通过实体的标识符进行解释的问题。
var faculty = {
"teachers": {
"t1": ["s1","s2","s5"],
"t2": ["s2","s7","s9"]
},
"students": {
"s1": ["t1","t2"],
"s2": ["t2","t7"]
}
}
For example:
例如:
alert("Teacher t1's students are: " + faculty.teachers.t1.toString() );
alert("Student s2's teachers are: " + faculty.students.s2.toString() );
alert("Student s2's first teacher's students are: " + faculty.teachers[faculty.students.s2[0]].toString() );
#2
3
You could make an array of pairs describing the relations as a directed graph?
你可以做一个成对的数组把关系描述成有向图?
[// from , to
["s1", "t1"],
["s1", "t2"],
["s2", "t2"],
["s2", "t4"],
["t1", "s1"],
["t1", "s2"],
["t1", "s3"],
["t1", "s4"]
]
It wouldn't be concise. But it would describe your dataset.
它不会是简洁。但它会描述你的数据集。
#1
12
As JSON does not have a concept of references, you should not need to worry about them. That which defines what counts as a relation between teachers and students lies outside of the data, i.e. is simply a matter of your interpretation during runtime, through the entities' identifiers.
由于JSON没有引用的概念,所以不需要担心它们。定义什么是老师和学生之间的关系不在数据之内,也就是说仅仅是你在运行时通过实体的标识符进行解释的问题。
var faculty = {
"teachers": {
"t1": ["s1","s2","s5"],
"t2": ["s2","s7","s9"]
},
"students": {
"s1": ["t1","t2"],
"s2": ["t2","t7"]
}
}
For example:
例如:
alert("Teacher t1's students are: " + faculty.teachers.t1.toString() );
alert("Student s2's teachers are: " + faculty.students.s2.toString() );
alert("Student s2's first teacher's students are: " + faculty.teachers[faculty.students.s2[0]].toString() );
#2
3
You could make an array of pairs describing the relations as a directed graph?
你可以做一个成对的数组把关系描述成有向图?
[// from , to
["s1", "t1"],
["s1", "t2"],
["s2", "t2"],
["s2", "t4"],
["t1", "s1"],
["t1", "s2"],
["t1", "s3"],
["t1", "s4"]
]
It wouldn't be concise. But it would describe your dataset.
它不会是简洁。但它会描述你的数据集。