Ejemplo n.º 1
0
 private void btn1_Click(object sender, RoutedEventArgs e)
 {
     DateTime born = new DateTime(1955, 10, 28);
     int age = DateTime.Now.Year - born.Year-1 + ((DateTime.Now.Month >= born.Month && DateTime.Now.Day >= born.Day) ? 1 : 0);
     Student d = new Student(age,"Bill Gates");
     List<School> schools = new List<School>();
     schools.Add(new School("Harvard University", 1));
     schools.Add(new School(" Lakeside", 2));
     d.Education = schools;
     string jsonstr=d.GetJson();
     this.box.Text = jsonstr;
 }
        public async Task<List<Student>> ReadJsonSample()
        {
            string json = await GetJsonFromFile();
            try
            {
                if (json != null)
                {
                    List<Student> stu = new List<Student>();
                    JArray array = TransJObjectToJArray(json);
                    for (int i = 0; i < array.Count; i++)
                    {
                        JObject one = (JObject)array[i];
                        int age = Convert.ToInt32(one["age"].ToString());
                        string name = one["name"].ToString();
                        List<School> sch = new List<School>();
                        string edu = one["education"].ToString();
                        JArray eduarray = TransJObjectToJArray(edu);
                        for (int j = 0; j < eduarray.Count; j++)
                        {
                            JObject eduone = (JObject)eduarray[j];
                            string eduname = eduone["school"].ToString();
                            JObject oneschool = (JObject)JsonConvert.DeserializeObject(eduname);
                            string schoolname = oneschool["name"].ToString();
                            int rank = Convert.ToInt32(oneschool["rank"].ToString());
                            School schone = new School(schoolname, rank);
                            sch.Add(schone);
                        }
                        Student stuone = new Student(age, name);
                        stuone.Education = sch;
                        stu.Add(stuone);
                    }
                    return stu;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
            return null;

        }
Ejemplo n.º 3
0
 private void btn2_Click(object sender, RoutedEventArgs e)
 {
     string json= "{\"age\":\"233\",\"name\":\"Bill Gates\",\"education\":[{\"education\":{\"name\":\"Harvard University\",\"rank\":\"1\"}},{\"school\":{\"name\":\" Lakeside\",\"rank\":\"2\"}}]}";
     Student d = new Student(json);
     this.box.Text = $"{d.Age},{d.Name},{d.Education[0].Schoolname},{d.Education[1].Schoolname}";
 }