0

I have to query a webservice multiple times to get the following data: - schools - school classes - teachers

The webservice gives the data back as JSON string and I converted them to objects with the help of JSON.Net (I created objects for being used with the deserialization, I took the advice from this answer: How to map JSON to C# Objects):

  • class SchoolsRoot
    • public School[] Schools
  • class School
    • public string SchoolName
    • public string CountryCode
    • public int SchoolId
  • class SchoolClassesRoot
    • public SchoolClass[] SchoolClasses
  • class SchoolClass
    • public int SchoolClassId
    • public string SchoolClassName
    • public DateTime StartDate
    • public int ParentId (reference to School)
  • class TeachersRoot
    • public Teacher[] Teachers
  • class Teacher
    • public int TeacherId
    • public string TeacherFullname
    • public string TeacherEmail
    • public int ParentId (refernce to a single School Class)

When I query the webservice for the schools, classes and teachers I get all the objects but the objects are not in a hierarchy (teacher->school class->school) e.g. like I would have when using an entity framework data context.

What I would like to have is to have a simple way to get all teachers of a school for example, or get only schools from a specific country code.

My approach would be add properties for this to the object, maybe create some sort of parent container that holds all the data of the 3 "lists" and then iterate over the objects and manually create/set the references to the correct objects according to the "parentId".

The question is, is this a good way to set the references or are there more efficient ways? Is there maybe already some way using the deserialization of JSON.Net (I found nothing like in the documentation)?

Patric
  • 2,789
  • 9
  • 33
  • 60
  • Do the relationships exists in the backend database? For example, is there a link table between schools and teachers? If so, could you not just pass back the school id in Teacher? Or you could have a call to load a school and flags on the call determine what data is passed back. If you set RETURN_TEACHERS then all that school's teachers get passed back as a nested object in the school. Just a couple of ideas. – Adam Benson Sep 04 '17 at 11:24
  • I don't have access to the backend and cannot modify it, I can only access it with REST webservice calls. – Patric Sep 04 '17 at 11:52
  • 1
    Hmmm. You're limited then. As you suggest, building your own refs is the only way to go. Might be an idea to find out what's most useful to the end client and knock the data into that shape. – Adam Benson Sep 04 '17 at 13:46

0 Answers0