JSON在ASP.NET中使用方法
Json.NET的简单介绍 Json.NET只提供了服务器端的方法,主要有实现JSON文本与XML互相转换的类,有自定义读写JSON的JsonReader类和JsonWriter类,还有一个非自定义读写JSON的JavaScriptSerializer类。 ASP.NET AJAX中,服务器端由JavaScriptSerializer类的几个方法来用于实现序列化和反序列化能力。在Json.NET中,服务器端的序列化和反序列化能力则主要由JavaScriptConvert类的几个方法提供。本篇的例子只使用了JavaScriptConvert。 JavaScriptConvert
在客户端,Json.NET未提供支持。 下面我们尝试用这个API在ASP.NET中实现用JSON交互数据。 使用Json.NET在C/S中交互JSON数据的简单例子 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 – 自定义Web字体在IE9中不起作用
- asp.net-mvc – Asp.NET MVC 2预览2:Area的aspx命名空间问
- asp.net-mvc-4 – mvc4捆绑强类型捆绑包
- asp.net-mvc-5 – 当我点击外部提供商按钮时,为什么我的网站
- asp.net – 如何使用Web API属性路由传递DateTime参数?
- asp.net-mvc – ASP.net MVC成员资格重定向,具体取决于角色
- asp.net-mvc – 在MVC2中使用区域
- 在ASP.NET MVC中使用MySQL的AccountController
- asp.net-mvc – 在ASP.NET MVC Web应用程序中托管WCF服务
- ASP.Net 4.0 Web场缓存
- 在ASP.net C#中伪造浏览器请求
- asp.net-mvc – MVC – 它只是一个三层模型吗?
- ASP.NET站点移动到IIS7会导致页面输出中出现乱码
- asp.net-web-api – 基于参数类型重载Web api操作
- asp.net-mvc-3 – 不为CompareAttribute DataAnn
- asp.net – 什么是Silverlight,以及哪些类型的开
- asp-classic – 对经典ASP的多语言支持
- asp.net-mvc – 使用ASP.NET MVC和Razor的Knocko
- asp.net-core – 包恢复失败.回滚包更改
- asp-classic – 在VBScript中检查NULL的错误