C#中Json字符串的各种应用类实例讲解
在程序中任何事物及信息都可以用对象进行描述和承载,除了比较流行的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联系起来。先个给出几个命名空间
usingNewtonsoft.Json;
usingSystem.Runtime.Serialization;
usingSystem.ServiceModel;
usingNewtonsoft.Json.Linq;
usingSystem.IO;
usingSystem.Runtime.Serialization.Json;
这里需要注意的是这些命名空间在.net3.5及以上本版支持,同时必须在引用中添加System.Runtime.Serialization.dll的引用。
这里主要使用到的类如下:
JsonSerializer,StringWriter,StringReader,JsonWriter,JsonConvert,DataContractJsonSerializer。
1.实现自定义类与Json进行相互转化处理:
publicclassPerson { publicPerson() { } publicPerson(stringName,stringSex,intAge,stringAddress,PersonCharacterCharacter) { this.Name=Name; this.Sex=Sex; this.Age=Age; this.Address=Address; this.Character=Character; } publicstringName{get;set;} publicstringSex{get;set;} publicintAge{get;set;} publicstringAddress{get;set;} publicPersonCharacterCharacter{get;set;} } publicclassPersonCharacter { publicstringDaode{get;set;} publicstringWenhua{get;set;} publicstringXiuyang{get;set;} } publicvoidShowConvert() { Personperson=newPerson("lanar","男",24,"陕西",newPersonCharacter()); JsonSerializerserializer=newJsonSerializer(); StringWritersw=newStringWriter(); serializer.Serialize(newJsonTextWriter(sw),person); Console.WriteLine(sw.GetStringBuilder().ToString()); StringReadersr=newStringReader(@"{""Name"":""ppp"",""Age"":"12"}"); Personp1=(Project)serializer.Deserialize(newJsonTextReader(sr),typeof(Person)); Console.WriteLine(p1.Name+"=>"+p1.Age); }
2.契约方式:使用System.Runtime.Serialization.dll提供的DataContractJsonSerializer或者JsonReaderWriterFactory实现
publicclassPerson { publicPerson() { } publicPerson(stringName,stringSex,intAge,stringAddress,PersonCharacterCharacter) { this.Name=Name; this.Sex=Sex; this.Age=Age; this.Address=Address; this.Character=Character; } [DataMember] publicstringName{get;set;} [DataMember] publicstringSex{get;set;} [DataMember] publicintAge{get;set;} [DataMember] publicstringAddress{get;set;} [DataMember] publicPersonCharacterCharacter{get;set;} } publicclassPersonCharacter { publicstringDaode{get;set;} publicstringWenhua{get;set;} publicstringXiuyang{get;set;} } publicvoidShowConvert() { Personperson=newPerson("许展鹏","男",,"陕西",newPersonCharacter()); Personp=newPerson(){Name="四大圣地",Age=,Sex="男",Character=newPersonCharacter(){Daode="sds",Wenhua="dasd",Xiuyang="zzz"}}; DataContractJsonSerializerserializer=newDataContractJsonSerializer(p.GetType()); stringjsonText; try { using(MemoryStreamstream=newMemoryStream()) { serializer.WriteObject(stream,p); jsonText=Encoding.UTF.GetString(stream.ToArray()); Console.WriteLine(jsonText); } using(MemoryStreamms=newMemoryStream(Encoding.UTF.GetBytes(jsonText))) { DataContractJsonSerializerserializer=newDataContractJsonSerializer(typeof(Person)); Personp=(Person)serializer.ReadObject(ms); } } catch(Exceptionex) { thrownewException(ex.Message,ex); } }
使用契约方式必须在类及相关的属性添加相关的契约修饰符:[DataContract],[DataMember]内嵌的对象内部可以不用添加契约修饰符号的。
以上只是最常用的应用方式,对于有特殊需求的实现可以使用第三方实现的json转换类进行处理。在web页面可以使用 引入System.Web.Script.Serialization命名空间使用JavaScriptSerializer类实现简单的序列化。