void CreationExample() { const string jsonString = @"{""Students"":[{""Name"":""Paco"", ""Age"":14},{""Name"":""Joan"", ""Age"":8},{""Name"":""Frank"", ""Age"":10}],""Teacher"":{""Name"":""Arnold"", ""Age"":43}}"; /* * { * "Teacher": {"Name": "Arnold,"Age": 43 }, * "Students":[ * {"Name": "Paco", "Age": 14} * {"Name": "Joan", "Age": 8} * {"Name": "Frank", "Age": 10} * ] * } */ Classroom classObj = new Classroom { Teacher = new Person { Name = "Arnold", Age = 43 }, Students = new List <Person> { new Person { Name = "Paco", Age = 14 }, new Person { Name = "Joan", Age = 8 }, new Person { Name = "Frank", Age = 10 }, } }; JSON classroomJson = new JSONObject { { "Teacher", new JSONObject { { "Name", "Arnold" }, { "Age", 43 } } }, { "Students", new JSONArray { new JSONObject { { "Name", "Paco" }, { "Age", 14 } }, new JSONObject { { "Name", "Joan" }, { "Age", 8 } }, new JSONObject { { "Name", "Frank" }, { "Age", 10 } } } } }; JSON classroomJsonParsed = JSON.LoadString(jsonString); JSON classroomSerialized = JSON.ToJSON(classObj); Classroom classroomParsed = JSON.FromJSON <Classroom>(classroomJsonParsed); Debug.Log("The parsed JSON is equal to the control: " + (classroomJson == classroomJsonParsed)); Debug.Log("The serialized object is equal to the control: " + (classroomJson == classroomSerialized)); Debug.Log("The Parsed object is equal to the control: " + classroomParsed.Equals(classObj)); Debug.Log("The serialized JSON is: " + JSON.DumpString(classroomJson)); Debug.Log("First student's name is: " + classroomJson["Students"][0]["Name"]); }
private void btnSave_Click(object sender, RoutedEventArgs e) { if (!addNew && dgrMain.SelectedIndex == -1) { return; } Boolean hasError = false; if (addNew || !currId.Equals(classroom.Id)) { foreach (Classroom s in MainWindow.classrooms) { if (classroom.Equals(s)) { MessageBox.Show("ID allready exists. ID must be unique."); hasError = true; return; } } } if (classroom.Id.Equals("")) { MessageBox.Show("ID must be set."); hasError = true; return; } if (classroom.Opis.Equals("") || classroom.BrRadnihMesta.Equals("") || classroom.PrisustvoProjektora.Equals("") || classroom.PrisustvoTable.Equals("") || classroom.PrisustvoPametneTable.Equals("") || classroom.Os.Equals("") || classroom.Softver.Count == 0) { MessageBox.Show("One or more values doesn't set. All values must be set."); hasError = true; return; } foreach (Software s in classroom.Softver) { Console.WriteLine(s.Naziv); Console.WriteLine(classroom.Os); if (!s.Os.Equals(classroom.Os)) { if (!classroom.Os.Equals("Both") && !s.Os.Equals("Cross platform")) { softwares.Clear(); classroom.Softver.Clear(); softwareShow.Clear(); o.SelectedIndex = -1; foreach (Software ss in MainWindow.softwares) { if (!softwares.Contains(ss)) { softwareShow.Add(ss); } } MessageBox.Show("Classroom has " + classroom.Os + " and Softver that you chose has " + s.Os + ". This two values must be the same"); hasError = true; return; } } } int d; if (int.TryParse(brRadnihMesta.Text, out d)) { //valid } else { //invalid MessageBox.Show("Please enter a valid number for number of workers!"); return; } if (!hasError) { if (addNew) { Classroom c = new Classroom(); c.Copy(classroom); String s = ""; foreach (Software sp in classroom.Softver) { s += sp.Naziv + " "; Console.WriteLine("ddd " + s); } c.ImenaSoftvera = s; Console.WriteLine(c.Termini.Count); MainWindow.classrooms.Add(c); addNew = false; } else if (dgrMain.SelectedIndex != -1) { int sIndex = dgrMain.SelectedIndex; String s = ""; foreach (Software sp in classroom.Softver) { s += sp.Naziv + " "; } classroom.ImenaSoftvera = s; classroomShow[dgrMain.SelectedIndex].Copy(classroom); Classroom currClassroom = classroomShow[dgrMain.SelectedIndex]; bool b = false; for (int i = currClassroom.Termini.Count - 1; i > -1; i--) { Console.WriteLine(i); foreach (Subject sub in MainWindow.subjects) { if (sub.Naziv.Equals(currClassroom.Termini[i].Predmet)) { if (!currClassroom.PrisustvoProjektora && sub.PrisustvoProjektora) { b = true; break; } if (!currClassroom.PrisustvoTable && sub.PrisustvoTable) { b = true; break; } if (!currClassroom.PrisustvoPametneTable && sub.PrisustvoPametneTable) { b = true; break; } if (!currClassroom.Os.Equals("Both") && !sub.Os.Equals("Both")) { if (!currClassroom.Os.Equals(sub.Os)) { b = true; break; } } } } } if (b) { if (MessageBox.Show("This action will remove appointments on schedule, delete anyway?", "Delete classroom", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No) { //do no stuff return; } else { //do yes stuff for (int i = currClassroom.Termini.Count - 1; i > -1; i--) { Console.WriteLine(i); foreach (Subject sub in MainWindow.subjects) { if (sub.Naziv.Equals(currClassroom.Termini[i].Predmet)) { if (!currClassroom.PrisustvoProjektora && sub.PrisustvoProjektora) { Console.WriteLine("Subject has projector, but classroom don't!"); currClassroom.Termini.RemoveAt(i); sub.BrTermina += 1; break; } if (!currClassroom.PrisustvoTable && sub.PrisustvoTable) { Console.WriteLine("Subject has table, but classroom don't!"); currClassroom.Termini.RemoveAt(i); sub.BrTermina += 1; break; } if (!currClassroom.PrisustvoPametneTable && sub.PrisustvoPametneTable) { Console.WriteLine("Subject has smart table, but classroom don't!"); currClassroom.Termini.RemoveAt(i); sub.BrTermina += 1; break; } if (!currClassroom.Os.Equals("Both") && !sub.Os.Equals("Both")) { if (!currClassroom.Os.Equals(sub.Os)) { Console.WriteLine("Subject has " + sub.Os + " but classroom has " + currClassroom.Os); currClassroom.Termini.RemoveAt(i); sub.BrTermina += 1; break; } } } } } } } dgrMain.SelectedIndex = sIndex; } save(); } }