JSON串行化

大多数情况下,我们不会再Javascript中直接创建JSON字符串。而是,创建一些Javascript对象,这些对象和服务器上的.net对象对应。因此,可以在服务器上创建.net实体类,然后串行化成JSON格式,随后在浏览器中反串行化为Javascript对象。

Album.asmx

<%@ WebService Language="C#" Class="AlbumProxy" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Net;
using System.Xml.Serialization;
using System.Web.Script.Serialization;
public class Album
{
    private string _artist = String.Empty;
    private string _title = String.Empty;
    private int _releaseYear;
    private string[] _tracks = new string[16];
    private DateTime _dateTime = DateTime.Now;
    private string _personalInfo = "do not show this";

    [ScriptIgnore]
    public string PersonalInfo
    {
        get
        {
            return _personalInfo; 
        }
        set
        {
            _personalInfo = value; 
        }
    }

    public Album() { }

    public string Artist
    {
        get
        {
            return _artist; 
        }
        set
        {
            _artist = value; 
        }
    }

    public string Title
    {
        get
        {
            return _title; 
        }
        set
        {
            _title = value; 
        }
    }

    public int ReleaseYear
    {
        get
        {
            return _releaseYear; 
        }
        set
        {
            _releaseYear = value; 
        }
    }

    public string[] Tracks
    {
        get
        {
            return _tracks;
        }
        set
        {
            _tracks = value; 
        }
    }
}


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
[System.Web.Script.Services.ScriptService]
public class AlbumProxy  : System.Web.Services.WebService 
{
    private Album _album;
    
    public AlbumProxy()
    {
        _album = new Album();
        _album.Artist = "Phish";
        _album.Title = "A Picture of Nectar";
        _album.ReleaseYear = 1992;
        _album.Tracks.SetValue("Llama", 0);
        _album.Tracks.SetValue("Eliza", 1);
        _album.Tracks.SetValue("Cavern", 2);
        _album.Tracks.SetValue("Poor Heart", 3);
        _album.Tracks.SetValue("Stash", 4);
        _album.Tracks.SetValue("Manteca", 5);
        _album.Tracks.SetValue("Guelah Papyrus", 6);
        _album.Tracks.SetValue("Magilla", 7);
        _album.Tracks.SetValue("The Landlady", 8);
        _album.Tracks.SetValue("Glide", 9);
        _album.Tracks.SetValue("Tweezer", 10);
        _album.Tracks.SetValue("The Mango Song", 11);
        _album.Tracks.SetValue("Chalk Dust Torture", 12);
        _album.Tracks.SetValue("Faht", 13);
        _album.Tracks.SetValue("Catapult", 14);
        _album.Tracks.SetValue("Tweezer Reprise", 15);
    }
            
    [WebMethod]
    [XmlInclude(typeof(Album))]
    public object GetAlbum() {
        return _album;
    }

    [WebMethod]
    [XmlInclude(typeof(Album))]
    public object GetAlbumJSON()
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        return serializer.Serialize(_album); 
    }
}

 

ScriptProxy.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ScriptProxy.aspx.cs" Inherits="Chap9Project.ScriptProxy" %>
<%@ Import Namespace="System.Web.Script.Serialization" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function pageLoad() {
            AlbumProxy.GetAlbumJSON(completionJSON);
            AlbumProxy.GetAlbum(completionObject);
        }

        function completionJSON(result) {
            alert(result);
            var album = Sys.Serialization.JavaScriptSerializer.deserialize(result);
            $get(placeholder).innerHTML = album.Artist;
        }

        function completionObject(album) {
            alert(album);
            $get(placeholder2).innerHTML = album.ReleaseYear;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/Album.asmx" />
            </Services>
        </asp:ScriptManager>
        <div id="placeholder"></div>
        <br />
        <div id="placeholder2"></div>
    </div>
    </form>
</body>
</html>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。