日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

在ASP.NET中使用JSON

系統(tǒng) 2836 0

本篇將簡(jiǎn)單的介紹一個(gè)在.NET中實(shí)現(xiàn)JSON的API,然后使用該API做個(gè)C/S ASP.NET的小練習(xí)。

Json.NET的簡(jiǎn)單介紹

首先介紹一個(gè)為方便在.NET中使用JSON的API,Json.NET。它方便我們讀取從瀏覽器流向服務(wù)器的JSON對(duì)象,也方便在響應(yīng)流中寫入JSON對(duì)象。這里下載: Json.NET

Json.NET只提供了服務(wù)器端的方法,主要有實(shí)現(xiàn)JSON文本與XML互相轉(zhuǎn)換的類,有自定義讀寫JSON的JsonReader類和JsonWriter類,還有一個(gè)非自定義讀寫JSON的JavaScriptSerializer類。

ASP.NET AJAX中,服務(wù)器端由JavaScriptSerializer類的幾個(gè)方法來用于實(shí)現(xiàn)序列化和反序列化能力。在Json.NET中,服務(wù)器端的序列化 和反序列化能力則主要由JavaScriptConvert類的幾個(gè)方法提供。本篇的例子只使用了JavaScriptConvert。

  1. JavaScriptConvert
    • Json.NET中,這個(gè)類用于序列化和反序列化JavaScript對(duì)象。
    • 這個(gè)類有兩個(gè)方法:
      1. SerializeObject(object value, params JsonConverter[] converters),序列化,它有個(gè)重載方法SerializeObject(object value)
      2. DeserializeObject(string value, Type type),反序列化,它有個(gè)重載方法DeserializeObject(string value)

在客戶端,Json.NET未提供支持。如果需要?jiǎng)t可以結(jié)合使用上一篇“ What is JSON:初識(shí)JSON ”提到的json.js來處理客戶端的系列化與反序列化。

下面我們嘗試用這個(gè)API在ASP.NET中實(shí)現(xiàn)用JSON交互數(shù)據(jù)。

使用Json.NET在C/S中交互JSON數(shù)據(jù)的簡(jiǎn)單例子

  1. 先新建一個(gè)ASP.NET 網(wǎng)站。

  2. 將下載到的Binary文件夾中的Newtonsoft.Json.dll和Newtonsoft.Json.XML放入網(wǎng)站的bin文件,當(dāng)然要先新建bin文件夾。然后對(duì)dll添加引用。

  3. 切 換到設(shè)計(jì)模式,從標(biāo)準(zhǔn)工具箱向頁面上添加三個(gè)Label,Text分別為EmployeeID、EmployeeName、EmployeeInfo;三 個(gè)Textbox,ID分別為txtID、txtName、txtInfo;然后添加一個(gè)Button,ID為btnToJSONString,Text 為Invoke ToJSONString;然后添加一個(gè)Textbox,ID為txtJSON,Textmode為MultiLine,rows設(shè)為5;接著再分別添加 一個(gè)Button和Textbox,ID為btnToObject、txtStrEmployee,Button的Text為Invoke ToStrEmployee。
  4. 添加一個(gè)WebService項(xiàng)目。
    • 在ASP.NET中使用JSON

    • 編寫一個(gè)Employee類,然后兩個(gè)WebMethod,接著在項(xiàng)目中對(duì)該Web服務(wù)添加引用。代碼如下:

      using System;
      using System.Web;
      using System.Collections;
      using System.Web.Services;
      using System.Web.Services.Protocols;
      using Newtonsoft.Json;

      class Employee
      {
      private string []employeeInfo;

      public int EmployeeID;
      public string EmployeeName;
      public string []EmployeeInfo
      {
      get { return this .employeeInfo;}
      set { this .employeeInfo = value;}
      }
      }

      /**/ /// <summary>
      /// WebService的摘要說明
      /// </summary>
      [WebService(Namespace = " http://tempuri.org/ " )]
      [WebServiceBinding(ConformsTo
      = WsiProfiles.BasicProfile1_1)]
      public class WebService:System.Web.Services.WebService{

      public WebService(){

      // 如果使用設(shè)計(jì)的組件,請(qǐng)取消注釋以下行
      // InitializeComponent();
      }

      [WebMethod]
      public string ToJSONString( int employeeID, string employeeName, string []employeeInfo)
      {
      Employeeemployee
      = new Employee();
      employee.EmployeeID
      = employeeID;
      employee.EmployeeName
      = employeeName;
      employee.EmployeeInfo
      = employeeInfo;

      return JavaScriptConvert.SerializeObject(employee);
      }

      [WebMethod]
      public string ToStrEmployee( string strJSON)
      {
      EmployeedecerializedEmployee
      = (Employee)JavaScriptConvert.DeserializeObject(strJSON, typeof (Employee));
      return " ID: " + decerializedEmployee.EmployeeID + " "
      + " Name: " + decerializedEmployee.EmployeeName + " "
      + " Info: " + decerializedEmployee.EmployeeInfo.ToString();
      }
      }

成員的屬性類型分別為數(shù)字、字符串和數(shù)組。

5、對(duì)兩個(gè)Button編寫事件代碼

protected void btnToJSONString_Click( object sender,EventArgse)
{
MyServ.WebServiceMyWebServ
= new MyServ.WebService();
string employeeJSON = MyWebServ.ToJSONString(Int32.Parse(txtID.Text),txtName.Text,txtInfo.Text.Split( ' , ' ));
txtJSON.Text
= employeeJSON;
}
protected void btnToStrEmployee_Click( object sender,EventArgse)
{
MyServ.WebServiceMyWevServ
= new MyServ.WebService();
string strEmployee = MyWevServ.ToStrEmployee(txtJSON.Text);
txtStrEmployee.Text
= strEmployee;
}

6、 按Ctrl + F5運(yùn)行;在EmployeeID、EmployeeName、EmployeeInfo中輸入123、Hunts.C及一些個(gè)人信息(用逗號(hào)隔開);點(diǎn) 擊Invoke ToJSONString,經(jīng)服務(wù)器端序列化后,結(jié)果在txtJSON文本框中;然后點(diǎn)擊Invoke ToStrEmployee,此時(shí)txtJSON文本框中的JSON文本傳輸給服務(wù)器端,服務(wù)器端讀取該JSON并反序列化成對(duì)象,而后在 txtStrEmployee中寫入Employee的成員值。

在ASP.NET中使用JSON







Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1400818

在ASP.NET中使用JSON


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 绩溪县| 天津市| 新野县| 奉贤区| 城固县| 营山县| 姚安县| 施秉县| 彩票| 衢州市| 和静县| 盐亭县| 安泽县| 吉木萨尔县| 怀来县| 吉隆县| 长春市| 沙坪坝区| 新绛县| 扶绥县| 皮山县| 滦平县| 高阳县| 宁城县| 香河县| 珲春市| 分宜县| 肇庆市| 枣阳市| 来宾市| 巨野县| 荥阳市| 乌审旗| 甘谷县| 陆河县| 广德县| 承德市| 临泉县| 阜城县| 福建省| 桃源县|