在程序中任何事物及信息都可以用对象进行描述和承载,除了比较流行的xml之外,还有一种简洁快速处理目标信息的形式那就是Json格式。
首先Json格式有自己固定的格式:例如person这个对象就可以简单的表示成{"name":"xxxx","age":12,"sex":"男"}形式。
Json格式的描述如下:先用{}包含需要表达的对象信息,在{}里使用PropertyName:Value进行对象属性的描述,同时Json字符串可以实现多层嵌套。例如:Json数据:[{PropertyName:Value,PropertyName:Value},{PropertyName:Value,PropertyName:Value}],Json嵌套格式:{PropertyName:Value,PropertyName:{PropertyName:Value,PropertyName:Value}},理论上可以无限嵌套,但是这里建议嵌套最好不要超过3层。
对Json格式有了了解之后,那我们如何在C#中使用Json以及将已经实现的对象与Json联系起来。先个给出几个命名空间
using Newtonsoft.Json;
using System.Runtime.Serialization;
using System.ServiceModel;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Runtime.Serialization.Json;
这里需要注意的是 这些命名空间在 .net3.5及以上本版支持,同时必须在引用中添加 System.Runtime.Serialization.dll的引用。
这里主要使用到的类如下:
JsonSerializer ,StringWriter,StringReader ,JsonWriter,JsonConvert, DataContractJsonSerializer。
1.实现自定义类与Json进行相互转化处理:
public class Person { public Person() { } public Person(string Name, string Sex, int Age, string Address, PersonCharacter Character) { this.Name = Name; this.Sex = Sex; this.Age = Age; this.Address = Address; this.Character = Character; } public string Name { get; set; } public string Sex { get; set; } public int Age { get; set; } public string Address { get; set; } public PersonCharacter Character { get; set; } } public class PersonCharacter { public string Daode { get; set; } public string Wenhua { get; set; } public string Xiuyang { get; set; } } public void ShowConvert() { Person person = new Person("lanar", "男", 24, "陕西", new PersonCharacter()); JsonSerializer serializer = new JsonSerializer(); StringWriter sw = new StringWriter(); serializer.Serialize(new JsonTextWriter(sw), person ); Console.WriteLine(sw.GetStringBuilder().ToString()); StringReader sr = new StringReader(@"{""Name"":""ppp"", ""Age"":"12"}"); Person p1 = (Project)serializer.Deserialize(new JsonTextReader(sr), typeof(Person)); Console.WriteLine(p1.Name+ "=>" + p1.Age); }
2. 契约方式:使用System.Runtime.Serialization.dll提供的DataContractJsonSerializer或者 JsonReaderWriterFactory实现
public class Person { public Person() { } public Person(string Name, string Sex, int Age, string Address, PersonCharacter Character) { this.Name = Name; this.Sex = Sex; this.Age = Age; this.Address = Address; this.Character = Character; } [DataMember] public string Name { get; set; } [DataMember] public string Sex { get; set; } [DataMember] public int Age { get; set; } [DataMember] public string Address { get; set; } [DataMember] public PersonCharacter Character { get; set; } } public class PersonCharacter { public string Daode { get; set; } public string Wenhua { get; set; } public string Xiuyang { get; set; } } public void ShowConvert() { Person person = new Person("许展鹏", "男", , "陕西", new PersonCharacter()); Person p = new Person() { Name = "四大圣地", Age = , Sex = "男", Character = new PersonCharacter() { Daode="sds", Wenhua="dasd", Xiuyang="zzz"} }; DataContractJsonSerializer serializer = new DataContractJsonSerializer(p.GetType()); string jsonText; try { using (MemoryStream stream = new MemoryStream()) { serializer.WriteObject(stream, p); jsonText = Encoding.UTF.GetString(stream.ToArray()); Console.WriteLine(jsonText); } using (MemoryStream ms = new MemoryStream(Encoding.UTF.GetBytes(jsonText))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Person)); Person p = (Person)serializer.ReadObject(ms); } } catch (Exception ex ) { throw new Exception(ex.Message,ex); } }
使用契约方式必须在类及相关的属性添加相关的契约修饰符: [DataContract], [DataMember] 内嵌的对象内部可以不用添加契约修饰符号的。
以上只是最常用的应用方式,对于有特殊需求的实现可以使用第三方实现的json转换类进行处理。在web页面可以使用 引入System.Web.Script.Serialization命名空间使用 JavaScriptSerializer类实现简单的序列化。