Json序列化更新好友列表

一、概述

使用Newtonsoft.Json开源库进行序列化

 

二、代码

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JsonStudy
{
    public class Student
    {
        public string Name;
        public int Age;
        public Student(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }
        public Student()
        {

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> studentList = new List<Student>()
            {

                new Student() { Name = "Tom", Age = 33},
                 new Student { Name = "Mary", Age = 36},
                new Student("Alice", 25)
            };
            string str = JsonConvert.SerializeObject(studentList);

            List<Student> list = JsonConvert.DeserializeObject<List<Student>>(str);
            Console.WriteLine(str);
        }
    }
}

 

posted on 2019-06-20 12:32  缘惜  阅读(304)  评论(0编辑  收藏  举报