rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » Javascript

jquery json

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" ValidateRequest="false"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script src="Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function test() {

            $.ajax({
                url: "Default.aspx?action=test",
                cache: false,
                dataType: 'json',
                complete: function () { window.location.href = window.location.href; },
                beforeSend: function (x) {
                    x.setRequestHeader("Content-Type", "application/json; charset=utf-8");                 success: function (jsonString) {
                    alert(jsonString);
                    alert(jsonString.ID);
                    alert(jsonString.Sex);
                    alert(jsonString.Name);

                    $.each(jsonString.Columns, function (index, item) {
                        alert(item.Name);
                        alert(item.Value);
                        alert(item.Type);                 }             );     </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>

    <input type="button" value="查看对象" onclick="test()" />
</asp:Content>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ViewERP.Customization;
using Newtonsoft.Json;

namespace WebApplication1     public partial class _Default : System.Web.UI.Page         public string json;
        protected void Page_Load(object sender, EventArgs e)
        {

            switch(this.Request.QueryString["action"])                 case "test":

                json = JsonConvert.SerializeObject(Init());
                Response.Clear();
                Response.Write(json);
                Response.End();
                break;
         }

           //MyUser newmu = JsonConvert.DeserializeObject<MyUser>(json);

           //Response.Write( "\n" +newmu.ID);
           //Response.Write("\n" + newmu.Name);
           //Response.Write("\n" + newmu.Sex);         }

        public MyUser Init()             MyUser mu = new MyUser();
            mu.ID = Guid.NewGuid().ToString();
            mu.Name = "quanxuan";
            mu.Sex = true;

            IList<Column> columns = new List<Column>();
            columns.Add(new Column("test1"));
            columns.Add(new Column("test2"));
            columns.Add(new Column("test3"));
            columns.Add(new Column("test4", "test4"));
            columns.Add(new Column("test5", "32", "System.Int32"));

            mu.Columns = columns;

            return mu;     }

    public class MyUser         private string m_name;
        public string Name             get;
            set;         private string m_ID;
        public string ID             get;
            set;         private bool m_sex;
        public bool Sex             get;
            set;         private IList<Column> m_Columns;

        public IList<Column> Columns             get;
            set;     }

    public class Column         private string m_name;
        public string Name             get;
            set;
        }

        private string m_value;
        public string Value             get;
            set;         private string m_type;
        public string Type             get;
            set;
        }

        public Column(string name,string value="",string type = "System.String")             Name = name;
            Value = value;
            Type = type;     }
}

顶一下
(0)
踩一下
(0)