《JSON序列化》二
2016-04-06 10:46
295 查看
我们系统中的常用类型,都有对应的序列化器来支持。至于客户端和服务器端的远程调用,我们通过Asp.net AJAX扩展的web service来实现,先看看服务器端的代码:
namespace StepByStep.Forms
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class AJAXService : System.Web.Services.WebService
{
public class WebServerInfo
{
public DateTime ServerTime
{
get;
set;
}
//如果想在序列化时忽略此属性,请打开下一行的注释
//[ScriptIgnore]
public string ServerInformation
{
get;
set;
}
}
[WebMethod]
public WebServerInfo GetServerInfo()
{
WebServerInfo result = new WebServerInfo();
result.ServerTime = DateTime.Now;
result.ServerInformation = GetServerInformation();
return result;
}
private static string GetServerInformation()
{
StringBuilder strB = new StringBuilder();
using (TextWriter writer = new StringWriter(strB))
{
writer.WriteLine("MachineName: {0}", Environment.MachineName);
writer.WriteLine("OS Version: {0}", Environment.OSVersion.VersionString);
writer.WriteLine("Is 64 bits: {0}", Environment.Is64BitOperatingSystem.ToString());
writer.WriteLine("Processor Count: {0}", Environment.ProcessorCount);
}
return strB.ToString();
}
}
}
再看看客户端的代码:
<asp:ScriptManager runat="server" ID="scriptManager" EnableScriptGlobalization="true">
<Services>
<asp:ServiceReference Path="~/Forms/AJAXService.asmx" />
</Services>
</asp:ScriptManager>
<SOA:SubmitButton runat="server" Text="Get Server Info" AsyncInvoke="onGetServerInfo" />
<script type="text/javascript">
function onGetServerInfo() {
StepByStep.Forms.AJAXService.GetServerInfo(onGetServerInfoSuccess, onFailed);
//这个名字空间需要和服务器端对应
return false;
}
function onGetServerInfoSuccess(serverInfo) {
$get("serverInfoText").innerText = serverInfo.ServerTime;
$get("serverInfoText").innerText += "\n" + serverInfo.ServerInformation;
SubmitButton.resetAllStates();
}
function onFailed(e) {
SubmitButton.resetAllStates();
$showError(e);
}
</script>
在这个过程中,涉及到的对象序列化,都会遵循Asp.net AJAX的JSON序列化机制。关于JSON序列化,还有很多等待着自己去研究,继续钻研......
namespace StepByStep.Forms
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class AJAXService : System.Web.Services.WebService
{
public class WebServerInfo
{
public DateTime ServerTime
{
get;
set;
}
//如果想在序列化时忽略此属性,请打开下一行的注释
//[ScriptIgnore]
public string ServerInformation
{
get;
set;
}
}
[WebMethod]
public WebServerInfo GetServerInfo()
{
WebServerInfo result = new WebServerInfo();
result.ServerTime = DateTime.Now;
result.ServerInformation = GetServerInformation();
return result;
}
private static string GetServerInformation()
{
StringBuilder strB = new StringBuilder();
using (TextWriter writer = new StringWriter(strB))
{
writer.WriteLine("MachineName: {0}", Environment.MachineName);
writer.WriteLine("OS Version: {0}", Environment.OSVersion.VersionString);
writer.WriteLine("Is 64 bits: {0}", Environment.Is64BitOperatingSystem.ToString());
writer.WriteLine("Processor Count: {0}", Environment.ProcessorCount);
}
return strB.ToString();
}
}
}
再看看客户端的代码:
<asp:ScriptManager runat="server" ID="scriptManager" EnableScriptGlobalization="true">
<Services>
<asp:ServiceReference Path="~/Forms/AJAXService.asmx" />
</Services>
</asp:ScriptManager>
<SOA:SubmitButton runat="server" Text="Get Server Info" AsyncInvoke="onGetServerInfo" />
<script type="text/javascript">
function onGetServerInfo() {
StepByStep.Forms.AJAXService.GetServerInfo(onGetServerInfoSuccess, onFailed);
//这个名字空间需要和服务器端对应
return false;
}
function onGetServerInfoSuccess(serverInfo) {
$get("serverInfoText").innerText = serverInfo.ServerTime;
$get("serverInfoText").innerText += "\n" + serverInfo.ServerInformation;
SubmitButton.resetAllStates();
}
function onFailed(e) {
SubmitButton.resetAllStates();
$showError(e);
}
</script>
在这个过程中,涉及到的对象序列化,都会遵循Asp.net AJAX的JSON序列化机制。关于JSON序列化,还有很多等待着自己去研究,继续钻研......
相关文章推荐
- 《JSON序列化》一
- JS密码生成与强度检测完整实例(附demo源码下载)
- js--语法--for和for-in;访问对象的属性.和[]的区别
- JS隐藏gridview最后一列
- JSOI2015
- <json基础教程>
- JS控制伪元素的方法汇总
- DataTable 对象 转换为Json 字符串
- JS中六种数据类型(一)——undefined
- 全屏滚动插件pagePiling.js
- 自适应滑动插件swiper.js
- FullPage.js参数和函数详解
- 页面加载进度条pace.js
- 轻松实现全屏滚动-FullPage.js
- 跨浏览器本地存储-Store.js
- JS中浮点数运算不精准
- JS实现把鼠标放到链接上出现滚动文字的方法
- js中的“十二行诗” ----12行JS代码的DoS攻击分析及其防御
- 使用 AMD、CommonJS 及 ES Harmony 编写模块化的 JavaScript
- Content Editor Webpart(三)使用JSOM