星空网 > 软件开发 > ASP.net

C#中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实现

C#中Json字符串的各种应用类C#中Json字符串的各种应用类
 1  [DataContract] 2   public class Person 3   { 4     public  Person() 5     { 6     } 7     public Person(string Name, string Sex, int Age, string Address, PersonCharacter Character) 8     { 9       this.Name = Name;10       this.Sex = Sex;11       this.Age = Age;12       this.Address = Address;13       this.Character = Character;14     }15     [DataMember]16     public string Name { get; set; }17 18     [DataMember]19     public string Sex { get; set; }20 21     [DataMember]22     public int Age { get; set; }23 24       [DataMember]25     public string Address { get; set; }26 27     [DataMember]28     public PersonCharacter Character { get; set; }29 30   }31 32   public class PersonCharacter33   {34 35 36     public string Daode { get; set; }37 38 39     public string Wenhua { get; set; }40 41 42     public string Xiuyang { get; set; }43   }44 45 46     public void ShowConvert()47     {48       Person person = new Person("许展鹏", "男", 24, "陕西", new PersonCharacter());49 50       Person p = new Person() { Name = "四大圣地", Age = 22, Sex = "男", Character = new PersonCharacter() { Daode="sds", Wenhua="dasd", Xiuyang="zzz"} };51       DataContractJsonSerializer serializer = new DataContractJsonSerializer(p.GetType());52       string jsonText;53 54       try55       {56         using (MemoryStream stream = new MemoryStream())57         {58           serializer.WriteObject(stream, p);59           jsonText = Encoding.UTF8.GetString(stream.ToArray());60           Console.WriteLine(jsonText);61         }62 63         using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonText)))64         {65           DataContractJsonSerializer serializer1 = new DataContractJsonSerializer(typeof(Person));66           Person p1 = (Person)serializer1.ReadObject(ms);67 68         }69       }70       catch (Exception ex )71       {72         73         throw new Exception(ex.Message,ex);74       }75     }

View Code

使用契约方式必须在类及相关的属性添加相关的契约修饰符: [DataContract], [DataMember] 内嵌的对象内部可以不用添加契约修饰符号的。

     以上只是最常用的应用方式,对于有特殊需求的实现可以使用第三方实现的json转换类进行处理。在web页面可以使用   引入System.Web.Script.Serialization命名空间使用 JavaScriptSerializer类实现简单的序列化

 

JSON官网:http://www.json.org/json-zh.html

独立JSON序列化:http://msdn.microsoft.com/zh-cn/library/bb412170.aspx

如何对JSON序列化和反序列化:http://msdn.microsoft.com/zh-cn/library/bb412179.aspx

 




原标题:C#中Json字符串的各种应用类

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

多个品牌严抓商标侵权,这些词汇不能出现在listing!:https://www.kjdsnews.com/a/569711.html
跨境电商必看!一文带你跑通TikTok涨粉变现全流程:https://www.kjdsnews.com/a/569712.html
再出手!Shopee停止与部分ERP合作:https://www.kjdsnews.com/a/569713.html
5个亚马逊listing标题编辑误区,让转化越来越差:https://www.kjdsnews.com/a/569714.html
深圳大卖被封登上央视,给卖家的启示是...:https://www.kjdsnews.com/a/569715.html
陷入死循环!删除违规产品失败,卖家账号仍然封停:https://www.kjdsnews.com/a/569716.html
大福地快捷酒店预订 大福酒店怎么走:https://www.vstour.cn/a/365187.html
三亚有哪些酒店值得入住?:https://www.vstour.cn/a/366173.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流