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).
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.
Now, as you know, JSON only represents one-to-many relationship in this form:
{ "s1" : [ "t1", "t2"], "s2" : [ "t2", "t4" ], "s3" : [ "t1", "t3", "t4"], ...}
Do you have any idea on how to do this?
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.
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() );
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With