如何在ASP.NET项目中正确的使用JSON

  介绍

这篇文章给大家介绍如何在ASP.NET项目中正确的使用JSON,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

<强> Json.NET的简单介绍
Json.NET只提供了服务器端的方法,主要有实现JSON文本与XML互相转换的类,有自定义读写JSON的JsonReader类和JsonWriter类,还有一个非自定义读写JSON的JavaScriptSerializer类。

ASP。净AJAX中,服务器端由JavaScriptSerializer类的几个方法来用于实现序列化和反序列化能力。在Json.NET中,服务器端的序列化和反序列化能力则主要由JavaScriptConvert类的几个方法提供。本篇的例子只使用了JavaScriptConvert。

<强> JavaScriptConvert
Json。网中,这个类用于序列化和反序列化的JavaScript对象。
这个类有<强>两个方法:

<李>

SerializeObject(对象的值,参数JsonConverter[]转换器)、序列化,它有个重载方法SerializeObject(对象值)

<李>

DeserializeObject(字符串值,输入类型),反序列化,它有个重载方法DeserializeObject(字符串值)

在客户端,Json.NET未提供支持。

下面我们尝试用这个API在ASP.NET中实现用JSON交互数据。

<强>使用Json.NET在C/S中交互JSON数据的简单例子
1,先新建一个ASP。净网站。

如何在ASP.NET项目中正确的使用JSON

2,将下载到的二进制文件夹中的Newtonsoft.Json.dll和Newtonsoft.Json。XML放入网站的本文件,当然要先新建本文件夹。然后对dll添加引用。

3,切换到设计模式,从标准工具箱向页面上添加三个标签、文本分别为EmployeeID, EmployeeName, EmployeeInfo;三个文本框,ID分别为txtID, txtName, txtInfo,然后添加一个按钮,ID为btnToJSONString,文本为调用ToJSONString;然后添加一个文本框,ID为txtJSON,文本模式为多行,行设为5,接着再分别添加一个按钮和文本框,ID为btnToObject, txtStrEmployee,按钮的文本为调用ToStrEmployee。

4,添加一个网络服务项目。

如何在ASP。净项目中正确的使用JSON

编写一个员工类,然后两个WebMethod,接着在项目中对该Web服务添加引用。代码如下:

using 系统;   using 包含;   using  System.Collections;   using  System.Web.Services;   using  System.Web.Services.Protocols;   using  Newtonsoft.Json;      class 员工   {   private 才能,string [], employeeInfo;   ,,   public 才能;int  EmployeeID;   public 才能;string  EmployeeName;   public 才能,string [], EmployeeInfo   {才能   ,,,get  {this.employeeInfo;, return }   ,,,set  {=, this.employeeInfo 价值;}   ,,}   }/* *////,& lt; summary>///,WebService 的摘要说明///,& lt;/summary>   网络服务(=Namespace “http://tempuri.org/"))   时间=[WebServiceBinding ConformsTo  WsiProfiles.BasicProfile1_1))   时间:public  class  WebService  System.Web.Services.WebService  {      public 才能;WebService  (), {      ,,,//如果使用设计的组件,请取消注释以下行,   ,,,//InitializeComponent (),,   ,,}      (WebMethod)才能   public 才能;string  ToJSONString (int  employeeID, string  employeeName,, string [], employeeInfo),   {才能   ,,,Employee  Employee =, new 员工();   ,,,employee.EmployeeID =, employeeID;   ,,,employee.EmployeeName =, employeeName;   ,,,employee.EmployeeInfo =, employeeInfo;      ,,,return  JavaScriptConvert.SerializeObject(员工);   ,,}      (WebMethod)才能   public 才能;string  ToStrEmployee (string  strJSON)   {才能   ,,,Employee  decerializedEmployee =,(员工)JavaScriptConvert.DeserializeObject (strJSON, typeof(员工));   ,,,return “ID:,“, +, decerializedEmployee.EmployeeID  +,“,“   ,,,,,+,“名字:,“,+,decerializedEmployee.EmployeeName  +,“,“   ,,,,,+,“信息:,“,+,decerializedEmployee.EmployeeInfo.ToString ();   ,,},   }

成员的属性类型分别为数字,字符串和数组。

5 <强>,对两个按钮编写事件代码

protected  void  btnToJSONString_Click (object ,发送方,EventArgs  e)   {才能   ,,,MyServ.WebService  MyWebServ =, new  MyServ.WebService ();   ,,,string  employeeJSON =, MyWebServ.ToJSONString (Int32.Parse (txtID.Text), txtName.Text,, txtInfo.Text.Split (& # 39; & # 39;));   ,,,txtJSON.Text =, employeeJSON;   ,,}   protected 才能;void  btnToStrEmployee_Click (object ,发送方,EventArgs  e)   {才能   ,,,MyServ.WebService  MyWevServ =, new  MyServ.WebService ();   ,,,string  strEmployee =, MyWevServ.ToStrEmployee (txtJSON.Text);   ,,,txtStrEmployee.Text =, strEmployee;   以前,,}

如何在ASP.NET项目中正确的使用JSON