getJson 类对象的序列化.而且类对象包含数组的属性

来源:互联网 发布:软件测试系统 编辑:程序博客网 时间:2024/05/16 00:53

转载:http://blog.csdn.net/zj1103/article/details/2822215

<script. type="text/javascript">
    $(document).ready(function (){
        $("#btnOK").click(function (){
            $.getJSON(
                "Handler.ashx",
                {},
                function(json){
                    $("#list").append("<li>id:"+json.EmployeeId+"|Name:"+json.EmployeeName+"|年龄:"+json.EmployeeInfo[0]+"|身高:"+json.EmployeeInfo[1]+"|体重:"+json.EmployeeInfo[2]+"</li>");
                }
            )
        })
    })
</script>
<body>
    <input id="btnOK" value="加载数据" type="button"/>
    <ul id="list">
        
    </ul>
</body>
</html>

---------------------
Handler.ashx服务器端处理请求的代码
<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Runtime.Serialization;
using Newtonsoft.Json;

public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Write(ReturnResult());  
    }

    public string ReturnResult() {
        Employee employee = new Employee();
        employee.EmployeeId = 1;
        employee.EmployeeName = "yang";
        employee.EmployeeInfo = "25,170cm,55kg".Split(',');
        string jsonstr = JavaScriptConvert.SerializeObject(employee);
        return jsonstr;
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

    class Employee
    {
        public int EmployeeId;
        public string EmployeeName;
        private string[] employeeInfo;
        public string[] EmployeeInfo
        {
            get { return employeeInfo; }
            set { employeeInfo = value; }
        }
    }
}

原创粉丝点击