JSON在ASP.NET中使用方法

所属分类: 网络编程 / ASP.NET 阅读数: 2043
收藏 0 赞 0 分享

Json.NET的简单介绍
首先介绍一个为方便在.NET中使用JSON的API,Json.NET。它方便我们读取从浏览器流向服务器的JSON对象,也方便在响应流中写入JSON对象。

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

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

JavaScriptConvert
Json.NET中,这个类用于序列化和反序列化JavaScript对象。
这个类有两个方法:

  • SerializeObject(object value, params JsonConverter[] converters),序列化,它有个重载方法SerializeObject(object value)
  • DeserializeObject(string value, Type type),反序列化,它有个重载方法DeserializeObject(string value)

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

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

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

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

3、切换到设计模式,从标准工具箱向页面上添加三个Label,Text分别为EmployeeID、EmployeeName、EmployeeInfo;三个Textbox,ID分别为txtID、txtName、txtInfo;然后添加一个Button,ID为btnToJSONString,Text为Invoke ToJSONString;然后添加一个Textbox,ID为txtJSON,Textmode为MultiLine,rows设为5;接着再分别添加一个Button和Textbox,ID为btnToObject、txtStrEmployee,Button的Text为Invoke ToStrEmployee。

4、添加一个WebService项目。

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

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 () {

    //如果使用设计的组件,请取消注释以下行 
    //InitializeComponent(); 
  }

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

    return JavaScriptConvert.SerializeObject(employee);
  }

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

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

5、对两个Button编写事件代码

protected void btnToJSONString_Click(object sender, EventArgs e)
  {
    MyServ.WebService MyWebServ = 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, EventArgs e)
  {
    MyServ.WebService MyWevServ = new MyServ.WebService();
    string strEmployee = MyWevServ.ToStrEmployee(txtJSON.Text);
    txtStrEmployee.Text = strEmployee;
  } 

6、按Ctrl + F5运行;在EmployeeID、EmployeeName、EmployeeInfo中输入123、Hunts.C及一些个人信息(用逗号隔开);点击Invoke ToJSONString,经服务器端序列化后,结果在txtJSON文本框中;然后点击Invoke ToStrEmployee,此时txtJSON文本框中的JSON文本传输给服务器端,服务器端读取该JSON并反序列化成对象,而后在txtStrEmployee中写入Employee的成员值。

                 

在ASP.NET中如何使用JSON就介绍到这里,希望这篇文章对大家的学习有所帮助。

更多精彩内容其他人还在看

asp.net 页面间传值与跳转的区别

通过Server.Transfer("b.aspx") 与Response.Redirect("b.aspx")的区别
收藏 0 赞 0 分享

ASP.NET Gridview与checkbox全选、全不选实现代码

ASP.NET Gridview checkbox全选与全不选实现代码,其实原理就是利用js来实现的,但需要简单的设置下回传。
收藏 0 赞 0 分享

ASP.NET DropDownList控件的使用方法

ASP.NET DropDownList控件的使用方法,学习asp.net的朋友没用过这个控件的朋友可以参考下。
收藏 0 赞 0 分享

一些.NET对多线程异常处理技巧分享

多线程应用,在实际的项目或产品开发中,原则上来说,应该尽量避免,但是在强调用户体验的要求下或开发平台的限制下(如 Silverlight Socket 通讯),我们不得不用多线程。
收藏 0 赞 0 分享

ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法

同一相站点,有些页面的客户端验证能工作,而有些死活不行。打开页面就出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null错误
收藏 0 赞 0 分享

asp.net 通用分页显示辅助类(改进版)

在使用ASP.NET编程时,如果不用控件的默认分页功能,想自己搞一个,可以看看本文的asp.net通用分页显示辅助类哦。
收藏 0 赞 0 分享

微软 Visual Studio 2010官方下载地址给大家

昨天VS2010在网上报道都已经发布了,现在今天在网上找到Visual Studio 2010官方下载地址,提供给大家下载。
收藏 0 赞 0 分享

Javascript 直接调用服务器C#代码 ASP.NET Ajax实例

近来总有一些朋友会问到一些入门的问题,把这些问题整理一下,写出来。在以前的文章里,曾经利用纯JS编写过Ajax引擎,在真正开发的时候,大家都不喜欢以这种低效率的方式开发,利用MS Ajax的集成的引擎,可以简单不少工作。
收藏 0 赞 0 分享

ASP.NET 页面刷新的实现方法(包括html,js)

ASP.NET 页面刷新的实现方法,比较全了, 包括html与js下的实现方法。
收藏 0 赞 0 分享

asp.net 无刷新翻页就是这么简单

前两天看了一个自定义分页控件,和AspNetPager一样是实现IPostBackEventHandler接口,不过简洁许多,就想能不能实现ICallbackEventHandler接口做到无刷新分页呢?想到了就马上去做,终于,设想变成了现实!!
收藏 0 赞 0 分享
查看更多