zoukankan      html  css  js  c++  java
  • 用json格式返回自定义对象。(jQuery asp.net)

    客户端用一个html页面调用一个ashx文件(一般http处理程序),返回 json格式的自定义对象:

    html:--------------------------------------------------

    <!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>
        <title>ajax测试</title>

        <script src="js/jquery-1.2.3.js" type="text/javascript"></script><!-这里引用了jQuery框架->
    </head>
    <body>
    <script type="text/javascript">
    $(document).ready(function(){
               $("#Button2").click(function(){
               var url="handler.ashx?&name="+$("#Text1").val()+"&age="+$("#Text2").val();
               $.get(url,function(result){
               var obj=eval("("+result+")");
                                    alert("姓名:"+obj.Name+"\n"+"年龄:"+obj.Age);
                               })
                            })
    })
    </script>
        <input id="Button2" type="button" value="button" /><span lang="zh-cn">姓名:</span><input id="Text1"
            type="text" /><span lang="zh-cn">年龄:</span>
        <input id="Text2"
            type="text" />
           </body>
    </html>
    --------------------------------------------------

    handler.ashx文 件:--------------------------------------------------------

    <%@ WebHandler Language="C#" Class="Handler" %>
    using System;
    using System.Web;
    using System.Runtime.Serialization.Json;
    using System.Collections;
    using System.Runtime.Serialization;
    public class Handler : IHttpHandler {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string name = context.Request.Params["name"].ToString();
            string age = context.Request.Params["age"].ToString();
            person p1 = new person(name,age);


            DataContractJsonSerializer djson = new DataContractJsonSerializer(p1.GetType());//将对象序列化为 JavaScript 对象表示法 (JSON)


            djson.WriteObject(context.Response.OutputStream, p1);
        }
        public bool IsReusable {
            get {
                return false;
            }
        }
       [DataContract]//要序列化,一定要加这个属性
        public class person
        {
           [DataMember]//属性“DataMember”只在“property, indexer, field”声明中有效。
            public string Name="无名士";
            [DataMember]
           public string Age="0";
          
            public override string ToString()
            {
                return "姓名:" + Name + "年龄:" + Age;
            }
           
            public person(string name,string age)//自定义类person
            {
                this.Name = name;
                this.Age = age;
            }
            public person()
            { }
        }
    }

  • 相关阅读:
    78. Subsets
    93. Restore IP Addresses
    71. Simplify Path
    82. Remove Duplicates from Sorted List II
    95. Unique Binary Search Trees II
    96. Unique Binary Search Trees
    312. Burst Balloons
    程序员社交平台
    APP Store开发指南
    iOS框架搭建(MVC,自定义TabBar)--微博搭建为例
  • 原文地址:https://www.cnblogs.com/blsong/p/1705063.html
Copyright © 2011-2022 走看看