求一完整的解决方案
解决方案 »
- 修改个小错误 解决就送分
- 请教 关于 ASP的DROPDOWNLIST控件
- 在gridview里建了个新的模板列,处理选择行的命令。但是遇到了问题。请高手帮忙
- 简单的问题,急问
- volumeBar组件来控制声音的大小
- 一个关于读出数据后改变存放表的格式的问题
- asp.net连接DB2运行时提示"provider"未在本机注册怎么解决?
- 内容页中Repeater的Div获取
- 一个菜鸟的问题,请大家帮帮我.
- asp.net文本框的onclick事件怎么没有?
- NHibernate中需要使用多个数据库,怎么配置呢!谢谢高手指点一下
- 有人研究过extjs吗?访问webservices的身份认证怎么做啊
Jayrock 和 Jayrock.Json首先引入命名空间
using Jayrock.Json;其次,创建 JsonObject 对象,步骤如下:string strJsonText = @"{"cacheCount":1,"count":"34","slice":"5, 5","list":[1001598,1001601,1001605,1001609,1001612],"page":1,"error":200}";JsonReader reader = new JsonTextReader(new StringReader(strJsonText));JsonObject jsonObj = new JsonObject();
jsonObj.Import(reader);这样,就将一个文本的JSon数据转变成一个对象,如果要获取 count 的值,则可以这样string count = jsonObj["count"].ToString();但是有个问题,list 是一个数组,该如何获取呢?不用急,Jayrock已经为我们准备好了,来看using (JsonTextReader textReader = new JsonTextReader(new StringReader(jsonObj["list"].ToString())))
{
while (textReader.Read())
{
if (!string.IsNullOrEmpty(textReader.Text))
{
Response.Write(textReader.Text);
}
}
}将数组的内容再赋予一个JsonTextReader对象 ,利用其Read方法进行逐行读取就OK了当然,你也可以使用 JsonArray 对象,这里就不再叙述了
加eval()转换就就可以了JS要好好学习的啊
1、Client - helloworld.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Ajax Hello World</title>
<script type="text/javascript">
var xmlHttp;function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){'
xmlHttp = new XMLHttpRequest();
}
}
function startRequest(){
createXMLHttpRequest();:
try{
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "data.txt", true);
xmlHttp.send(null);
}catch(exception){
alert("您要访问的资源不存在!");
}
}
function handleStateChange(){
if(xmlHttp.readyState == 4){
if (xmlHttp.status == 200 || xmlHttp.status == 0){
// 取得返回字符串
var resp = xmlHttp.responseText;
// 构造返回JSON对象的方法
var func = new Function("return "+resp);
// 得到JSON对象
var json = func( );
// 显示返回结果- \+
alert("JSON's value: " + json.info + "(" + json.version + "v)");
};
})
}/
</script>
</head>
<body>
<div>
<input type="button" value="return ajax JSON's value"+
onclick="startRequest();" />
</div>
</body>.
</html>
2、Server - data.txt{: a
info: "hello world!",
version: "2.0"
}
1、Client - helloworld.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Ajax Hello World</title>
<script type="text/javascript">
var xmlHttp;function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){'
xmlHttp = new XMLHttpRequest();
}
}
function startRequest(){
createXMLHttpRequest();:
try{
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "data.txt", true);
xmlHttp.send(null);
}catch(exception){
alert("您要访问的资源不存在!");
}
}
function handleStateChange(){
if(xmlHttp.readyState == 4){
if (xmlHttp.status == 200 || xmlHttp.status == 0){
// 取得返回字符串
var resp = xmlHttp.responseText;
// 构造返回JSON对象的方法
var func = new Function("return "+resp);
// 得到JSON对象
var json = func( );
// 显示返回结果- \+
alert("JSON's value: " + json.info + "(" + json.version + "v)");
};
})
}/
</script>
</head>
<body>
<div>
<input type="button" value="return ajax JSON's value"+
onclick="startRequest();" />
</div>
</body>.
</html>
2、Server - data.txt
{
info: "hello world!",
version: "2.0"
}
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" onclick="showJSON()"></p>
<P><input type="button" value="修改" name="B1" onclick="showmodJSON()"></P>
<p><input type="button" value="showcar" name="B2" onclick="dojson()"></p>
<p><input type="button" value="evar" name="evar" onclick="myEval()"></p>
<p><input type="button" value="parsejoin" name="B5" onclick="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
}
}