1. js.js   
  2.   
  3.  function createxmlhttp()   
  4.  {   
  5.    var xmlhttp;   
  6.     if (window.ActiveXObject) {   
  7.       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   
  8.     }   
  9.     else if (window.XMLHttpRequest) {   
  10.       xmlhttp = new XMLHttpRequest();   
  11.     }   
  12.     return xmlhttp;   
  13.  }   
  14.   
  15. 3.htm   
  16.   
  17. <html>   
  18.   
  19. <head>   
  20. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">   
  21. <title>新建网页 1</title>   
  22.   
  23. <script type="text/javascript" src="json/json.js"></script>   
  24. <SCRIPT LANGUAGE="JavaScript" SRC="js/pjs.js"></SCRIPT>   
  25. <SCRIPT LANGUAGE="JavaScript" SRC="js/zz.js"></SCRIPT>   
  26. <script type="text/javascript">   
  27.   var xmlhttp;   
  28. //显示内容   
  29.   function Car(make, model, year, color)   
  30.   {   
  31.     this.make = make;   
  32.     this.model = model;   
  33.     this.year= year;   
  34.     this.color = color;   
  35.   }   
  36.   function getcarobject()   
  37.   {   
  38.     return new Car("中国","2312","2007","yellow");   
  39.   }   
  40.   function dojson()   
  41.   {   
  42.     var car = getcarobject();   
  43.     var carstr = car.toJSONString();   
  44.     alert(carstr);   
  45.        
  46.     xmlhttp = null;   
  47.    xmlhttp = createxmlhttp();   
  48.    url = "WebForm1.aspx?dt="+new Date().getTime();   
  49.     xmlhttp.open("POST", url, true);   
  50.     xmlhttp.onreadystatechange = handleStateChange;   
  51.     xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");   
  52.     xmlhttp.send(carstr);   
  53.   }   
  54.   function handleStateChange() {   
  55.     if (xmlhttp.readyState == 4) {   
  56.       if (xmlhttp.status == 200) {   
  57.         document.getElementById("testid").innerHTML = xmlhttp.responseText;   
  58.         alert("存档成功!");   
  59.       }   
  60.     }   
  61.   }   
  62.   //这表示一个user对象,拥有username, age, info, address 等属性。   
  63.   function showJSON() {       
  64.     var user =        
  65.     {        
  66.         "username":"andy",       
  67.         "age":20,       
  68.         "info": { "tel""123456""cellphone""98765"},       
  69.         "address":       
  70.             [       
  71.                 {"city":"beijing","postcode":"222333"},       
  72.                 {"city":"newyork","postcode":"555666"}       
  73.             ]       
  74.     }           
  75.     alert(user.username);       
  76.     alert(user.age);       
  77.     alert(user.info.cellphone);       
  78.     alert(user.address[0].city);       
  79.     alert(user.address[0].postcode);       
  80. }   
  81. //也可以用JSON来简单的修改数据,修改上面的例子   
  82. function showmodJSON() {       
  83.     var user =        
  84.     {        
  85.         "username":"andy",       
  86.         "age":20,       
  87.         "info": { "tel""123456""cellphone""98765"},       
  88.         "address":       
  89.             [       
  90.                 {"city":"beijing","postcode":"222333"},       
  91.                 {"city":"newyork","postcode":"555666"}       
  92.             ]       
  93.     }       
  94.            
  95.     alert(user.username);       
  96.     alert(user.age);       
  97.     alert(user.info.cellphone);       
  98.     alert(user.address[0].city);       
  99.     alert(user.address[0].postcode);       
  100.            
  101.     user.username = "Tom";       
  102.     alert(user.username);       
  103. }       
  104. //使用eval来转换JSON字符到Object   
  105. function myEval() {       
  106.     var str = '{ "name": "Violet", "occupation": "character" }';       
  107.     var obj = eval('(' + str + ')');       
  108.     alert(obj.toJSONString());       
  109. }   
  110. //或者使用parseJSON()方法   
  111. function myEval2() {       
  112.     var str = '{ "name": "Violet", "occupation": "character" }';       
  113.     var obj = str.parseJSON();       
  114.     alert(obj.toJSONString());       
  115. }   
  116. </script>   
  117. </head>   
  118.   
  119. <body>   
  120.   
  121. <form method="POST" action="WebForm1.aspx" target=_blank name="form1">   
  122.  <p><input type="button" value="显示" name="B3" onclick="showJSON()"></p>   
  123.  <P><input type="button" value="修改" name="B1" onclick="showmodJSON()"></P>   
  124.  <p><input type="button" value="showcar" name="B2" onclick="dojson()"></p>   
  125.  <p><input type="button" value="evar" name="evar" onclick="myEval()"></p>   
  126.  <p><input type="button" value="parsejoin" name="B5" onclick="myEval2()"></p>   
  127.  <p><input type="button" value="按钮" name="B6"></p>   
  128.  <p>   
  129.    <div id="testid">   
  130.    </div>   
  131.  </p>   
  132. </form>   
  133.   
  134. </body>   
  135.   
  136. </html>   
  137.   
  138. webform1.aspx   
  139.   
  140. using System;   
  141. using System.Collections;   
  142. using System.ComponentModel;   
  143. using System.Data;   
  144. using System.Drawing;   
  145. using System.Web;   
  146. using System.Web.SessionState;   
  147. using System.Web.UI;   
  148. using System.Web.UI.WebControls;   
  149. using System.Web.UI.HtmlControls;   
  150. using Jayrock;   
  151. using Jayrock.Json;   
  152. using Jayrock.Json.Conversion;   
  153. using System.IO;   
  154. using System.Text;   
  155.   
  156. namespace licai   
  157. {   
  158.  /// <summary>   
  159.  /// WebForm1 的摘要说明。   
  160.  /// </summary>   
  161.  public class WebForm1 : System.Web.UI.Page   
  162.  {   
  163.   private void Page_Load(object sender, System.EventArgs e)   
  164.   {   
  165.       // 在此处放置用户代码以初始化页面   
  166.       Stream instream = Page.Request.InputStream;//获取传入Http实体内容   
  167.       BinaryReader br = new BinaryReader(instream, System.Text.Encoding.UTF8);//转换流   
  168.       byte[] byt = br.ReadBytes((int)instream.Length);   
  169.       string caj = System.Text.Encoding.UTF8.GetString(byt);//转换为string类型   
  170.       par pa = (par)JsonConvert.Import(typeof(par),caj);//转换为JSON对应的类   
  171.       Response.Write(pa.Make + pa.Year+ pa.Model+ pa.Color);//输出从客户端发送的JSON内容   
  172. /*  
  173.       Stream instream = Page.Request.InputStream;  
  174.       System.IO.StreamReader streamReader = new StreamReader(instream, System.Text.Encoding.GetEncoding("GB2312"));  
  175.       //读取流字符串内容  
  176.       string content = @streamReader.ReadToEnd();  
  177.       JsonTextReader reader = new JsonTextReader(new StringReader(content));  
  178.       //以下是转换为数组  
  179.       //string[] continents = (string[]) JsonConvert.Import(typeof(string[]),content);  
  180.       par pa = (par)JsonConvert.Import(typeof(par),content);  
  181.  
  182.       Response.Clear();  
  183.       Response.Buffer= false;   
  184.       Response.Charset="GB2312";  
  185.       Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");  
  186.       Response.Write(pa.Make + pa.Year+ pa.Model+ pa.Color);  
  187. */  
  188. /*  
  189.       while (reader.Read())  
  190.       {  
  191.         Response.Write(reader.Text+"<br>");  
  192.       }  
  193. */  
  194.       Response.End();   
  195.   }   
  196.     public class par   
  197.     {   
  198.       private string make, model, year, color;   
  199.   
  200.       public string Make   
  201.       {   
  202.         get { return make; }   
  203.         set { make = value; }   
  204.       }   
  205.   
  206.       public string Model   
  207.       {   
  208.         get { return model; }   
  209.         set { model = value; }   
  210.       }   
  211.   
  212.       public string Year   
  213.       {   
  214.         get { return year; }   
  215.         set { year = value; }   
  216.       }   
  217.   
  218.       public string Color   
  219.       {   
  220.         get { return color; }   
  221.         set { color = value; }   
  222.       }   
  223.   
  224.     }   
  225.   
  226.   
  227.   #region Web 窗体设计器生成的代码   
  228.   override protected void OnInit(EventArgs e)   
  229.   {   
  230.    //   
  231.    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。   
  232.    //   
  233.    InitializeComponent();   
  234.    base.OnInit(e);   
  235.   }   
  236.      
  237.   /// <summary>   
  238.   /// 设计器支持所需的方法 - 不要使用代码编辑器修改   
  239.   /// 此方法的内容。   
  240.   /// </summary>   
  241.   private void InitializeComponent()   
  242.   {       
  243.    this.Load += new System.EventHandler(this.Page_Load);   
  244.   }   
  245.   #endregion   
  246.  }   
  247. }  
ja
posted on 2008-11-07 10:33  阳春  阅读(693)  评论(0)    收藏  举报