some thing about JSon
2009-12-31 10:41
337 查看
实体类Student
注意:必须使用DataContractAttribute对类进行标记,使用DataMemberAttribute类成员进行标记,否则该类无法被序列化。
对象转为JSON字符串
JSON字符串转为对象
文章转载自:http://www.mzwu.com/article.asp?id=1913
/// <summary> /// 学生实体类 /// </summary> [System.Runtime.Serialization.DataContract(Namespace="http://www.mzwu.com/")] public class Student { private string _Name; private int _Age; public Student(string name, int age) { _Name = name; _Age = age; } /// <summary> /// 姓名 /// </summary> [System.Runtime.Serialization.DataMember] public string Name { set {_Name = value;} get { return _Name; } } /// <summary> /// 年龄 /// </summary> [System.Runtime.Serialization.DataMember] public int Age { set { _Age = value; } get { return _Age; } } }
注意:必须使用DataContractAttribute对类进行标记,使用DataMemberAttribute类成员进行标记,否则该类无法被序列化。
对象转为JSON字符串
Student stu = new Student("张三", 20); System.Runtime.Serialization.Json.DataContractJsonSerializer json = new System.Runtime.Serialization.Json.DataContractJsonSerializer(stu.GetType()); using (MemoryStream stream = new MemoryStream()) { json.WriteObject(stream, stu); Response.Write(System.Text.Encoding.UTF8.GetString(stream.ToArray())); }
JSON字符串转为对象
System.Runtime.Serialization.Json.DataContractJsonSerializer json = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Student)); using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("{/"Age/":20,/"Name/":/"张三/"} "))) { Student stu = (Student)json.ReadObject(stream); Response.Write(string.Format("name:{0},age:{1}", stu.Name, stu.Age)); }
文章转载自:http://www.mzwu.com/article.asp?id=1913
相关文章推荐
- Some thing about Lambda Expression and NHB
- Some thing about kali linux lite
- some thing about struts (谈谈struts)
- some thing about shmget
- Some thing about Graph
- Some thing about android:visibility
- Some words about Qt DOM Node, Element and Attribut
- Some notes about the SAP 4.6C running on HP-UX
- umount失败:In some cases useful info about processes that use……
- Some Problems about Gem
- some points to be think about how to create your
- 横扫千军3.3a相关资料(Some infomation about Totala Anihiliation Chinese version 3.3a)
- About compiling some source code on centos
- Some thinking about scala concurrency compare to java
- Some tips about the life cycle of Maya thread pool
- Some errors about installation[continue to update]
- [转]some issues about push
- 转的文章,About JSON(javascript object notation) 详细介绍
- (zhuan) Some Talks about Dual Learning
- Some tips about Class in Python