zoukankan      html  css  js  c++  java
  • c# 读取json 写json 序列化与反序列化 .net 4.0

    class Program
        {
            static void Main(string[] args)
            {
                Serialize(); 
                Deserialize();
                Console.ReadLine();
            }
    
            static void Deserialize()
            {
                String str = "{\"Age\":20,\"Name\":\"张三\"}";
                DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Student));
                using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(str)))
                {
                    Student stu = (Student)json.ReadObject(stream);
                    Console.WriteLine(stu.Name+":"+stu.Age);
                }
            }
    
            static void Serialize()
            {
                Student stu = new Student("张三", 20);
                DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Student));
                using (MemoryStream stream = new MemoryStream())
                {
                    json.WriteObject(stream, stu);
                    String str = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                    Console.WriteLine(str);
                }
            }
        }
    [System.Runtime.Serialization.DataContract(Namespace = "http://www.mzwu.com/")]
        class Student
        {
            [System.Runtime.Serialization.DataMember]
            public String Name { get; set; }
            [System.Runtime.Serialization.DataMember]
            public int Age { get; set; }
    
            public Student(String name, int age)
            {
                this.Name = name;
                this.Age = age;
            }
        }
  • 相关阅读:
    感觉跟奇怪
    人多
    淡忘
    可疑
    js判断对象是否为空对象的几种方法
    互联网隐私泄漏
    清明时节
    垃圾mac
    【ES6】---JavaScript(二)
    【微信小程序】---Socket聊天功能实现
  • 原文地址:https://www.cnblogs.com/i80386/p/2592651.html
Copyright © 2011-2022 走看看