Jayrock RPC的Json例子教程

Jayrock RPC的Json例子教程
js.js

function createxmlhttp()
{
var xmlhttp;
if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}

3.htm

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 1</title>

<script type="text/javascript" src="json/json.js"></script>
<SCRIPT LANGUAGE="JavaScript" SRC="js/pjs.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="js/zz.js"></SCRIPT>
<script type="text/javascript">
var xmlhttp;
//显示内容
function Car(make, model, year, color)
{
this.make = make;
this.model = model;
this.year= year;
this.color = color;
}
function getcarobject()
{
return new Car("中国","2312","2007","yellow");
}
function dojson()
{
var car = getcarobject();
var carstr = car.toJSONString();
alert(carstr);

xmlhttp = null;
xmlhttp = createxmlhttp();
url = "WebForm1.aspx?dt="+new Date().getTime();
xmlhttp.open("POST", url, true);
xmlhttp.onreadystatechange = handleStateChange;
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlhttp.send(carstr);
}
function handleStateChange() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
document.getElementById("testid").innerHTML = xmlhttp.responseText;
alert("存档成功!");
}
}
}
//这表示一个user对象,拥有username, age, info, address 等属性。
function showJSON() {
var user =
{
"username":"andy",
"age":20,
"info": { "tel": "123456", "cellphone": "98765"},
"address":
[
{"city":"beijing","postcode":"222333"},
{"city":"newyork","postcode":"555666"}
]
}
alert(user.username);
alert(user.age);
alert(user.info.cellphone);
alert(user.address[0].city);
alert(user.address[0].postcode);
}
//也可以用JSON来简单的修改数据,修改上面的例子
function showmodJSON() {
var user =
{
"username":"andy",
"age":20,
"info": { "tel": "123456", "cellphone": "98765"},
"address":
[
{"city":"beijing","postcode":"222333"},
{"city":"newyork","postcode":"555666"}
]
}

alert(user.username);
alert(user.age);
alert(user.info.cellphone);
alert(user.address[0].city);
alert(user.address[0].postcode);

user.username = "Tom";
alert(user.username);
}
//使用eval来转换JSON字符到Object
function myEval() {
var str = '{ "name": "Violet", "occupation": "character" }';
var obj = eval('(' + str + ')');
alert(obj.toJSONString());
}
//或者使用parseJSON()方法
function myEval2() {
var str = '{ "name": "Violet", "occupation": "character" }';
var obj = str.parseJSON();
alert(obj.toJSONString());
}
</script>
</head>

<body>

<form method="POST" action="WebForm1.aspx" target=_blank name="form1">
<p><input type="button" value="显示" name="B3" οnclick="showJSON()"></p>
<P><input type="button" value="修改" name="B1" οnclick="showmodJSON()"></P>
<p><input type="button" value="showcar" name="B2" οnclick="dojson()"></p>
<p><input type="button" value="evar" name="evar" οnclick="myEval()"></p>
<p><input type="button" value="parsejoin" name="B5" οnclick="myEval2()"></p>
<p><input type="button" value="按钮" name="B6"></p>
<p>
<div id="testid">
</div>
</p>
</form>

</body>

</html>

webform1.aspx

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Jayrock;
using Jayrock.Json;
using Jayrock.Json.Conversion;
using System.IO;
using System.Text;

namespace licai
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Stream instream = Page.Request.InputStream;//获取传入Http实体内容
BinaryReader br = new BinaryReader(instream, System.Text.Encoding.UTF8);//转换流
byte[] byt = br.ReadBytes((int)instream.Length);
string caj = System.Text.Encoding.UTF8.GetString(byt);//转换为string类型
par pa = (par)JsonConvert.Import(typeof(par),caj);//转换为JSON对应的类
Response.Write(pa.Make + pa.Year+ pa.Model+ pa.Color);//输出从客户端发送的JSON内容
/*
Stream instream = Page.Request.InputStream;
System.IO.StreamReader streamReader = new StreamReader(instream, System.Text.Encoding.GetEncoding("GB2312"));
//读取流字符串内容
string content = @streamReader.ReadToEnd();
JsonTextReader reader = new JsonTextReader(new StringReader(content));
//以下是转换为数组
//string[] continents = (string[]) JsonConvert.Import(typeof(string[]),content);
par pa = (par)JsonConvert.Import(typeof(par),content);

Response.Clear();
Response.Buffer= false;
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.Write(pa.Make + pa.Year+ pa.Model+ pa.Color);
*/
/*
while (reader.Read())
{
Response.Write(reader.Text+"<br>");
}
*/
Response.End();
}
public class par
{
private string make, model, year, color;

public string Make
{
get { return make; }
set { make = value; }
}

public string Model
{
get { return model; }
set { model = value; }
}

public string Year
{
get { return year; }
set { year = value; }
}

public string Color
{
get { return color; }
set { color = value; }
}

}


#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值