$.toJSON的用法或把数组转换成json…
2013-12-25 14:42
393 查看
1. html页面全部代码
<html>
<head>
<title></title>
<script src="../../Scripts/jquery-1.4.1.min.js"
type="text/javascript"></script>
<script src="../../Scripts/JqueryJson.js"
type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#json").click(function () {
//数组里的字段的命名和类型要和一般处理程序里定义的类里的变量要一样
//否则会出问题
var postdata = new Array();
postdata[1] = { id: 1, number: "yes" };
postdata[2] = { id: 2, number: "no" };
var postData = $.toJSON(postdata); //把数组转换成json字符串
//将json字符串反序列化,这个只是测试一下数组是否转换成json字符串
var content = $.parseJSON(postData);
$.each(content, function () {
alert(this.number);
});
//post提交并处理
$.post("json.ashx", { "array": postData }, function (data, status)
{
if (status == "success") {
alert(data);
}
});
});
})
</script>
</head>
<body>
<input type="button" value="json"
id="json"/>
</body>
</html>
2.json.ashx页面全部代码
<%@ WebHandler Language="C#" Class="json"
%>
using System;
using System.Web;
using System.Web.Script.Serialization;
using System.Collections.Generic;
public class json : IHttpHandler {
public void
ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//接受出过来的值
string sun = context.Request["array"].ToString();
//实例化JavaScriptSerializer对象
JavaScriptSerializer jss = new JavaScriptSerializer();
List<array> a = new
List<array>();
//把json转换其他list<array>类型
a = jss.Deserialize(sun,
typeof(List<array>)) as
List<array>;
string meg=null;
foreach (var item in a)
{
meg += item.number;
}
context.Response.Write(meg);
}
public class array
{
public int id { get; set; }
public string number { get; set; }
}
public bool IsReusable {
get {
return false;
}
}
}
<html>
<head>
<title></title>
<script src="../../Scripts/jquery-1.4.1.min.js"
type="text/javascript"></script>
<script src="../../Scripts/JqueryJson.js"
type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#json").click(function () {
//数组里的字段的命名和类型要和一般处理程序里定义的类里的变量要一样
//否则会出问题
var postdata = new Array();
postdata[1] = { id: 1, number: "yes" };
postdata[2] = { id: 2, number: "no" };
var postData = $.toJSON(postdata); //把数组转换成json字符串
//将json字符串反序列化,这个只是测试一下数组是否转换成json字符串
var content = $.parseJSON(postData);
$.each(content, function () {
alert(this.number);
});
//post提交并处理
$.post("json.ashx", { "array": postData }, function (data, status)
{
if (status == "success") {
alert(data);
}
});
});
})
</script>
</head>
<body>
<input type="button" value="json"
id="json"/>
</body>
</html>
2.json.ashx页面全部代码
<%@ WebHandler Language="C#" Class="json"
%>
using System;
using System.Web;
using System.Web.Script.Serialization;
using System.Collections.Generic;
public class json : IHttpHandler {
public void
ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//接受出过来的值
string sun = context.Request["array"].ToString();
//实例化JavaScriptSerializer对象
JavaScriptSerializer jss = new JavaScriptSerializer();
List<array> a = new
List<array>();
//把json转换其他list<array>类型
a = jss.Deserialize(sun,
typeof(List<array>)) as
List<array>;
string meg=null;
foreach (var item in a)
{
meg += item.number;
}
context.Response.Write(meg);
}
public class array
{
public int id { get; set; }
public string number { get; set; }
}
public bool IsReusable {
get {
return false;
}
}
}
相关文章推荐
- AJAX $.toJSON的用法或把数组转换成json类型
- $.toJSON的用法或把数组转换成json类型
- $.toJSON的用法或把数组转换成json类型
- AJAX $.toJSON的用法或把数组转换成json类型
- $.toJSON的用法或把数组转换成json类型(转)
- $.toJSON的用法或把数组转换成json类型
- php数组转换js数组操作及json_encode的用法详解
- JSON用法之将PHP数组转JS数组,JS如何接收PHP数组
- jquery下json数组的操作用法实例
- Java中toArray的用法探究(java数组与list转换)
- 将json 数组转换为Map 对象;为[{},{},{}]此类字; 则返回每个{}对象的map;
- JSON数组形式字符串转换为Map数组(转为其他的Bean的话,请参考自行变形)
- json和php数组 格式的互相转换
- 将Ext.Json.Store对象、数组对象转换称Json对象
- c#将json串转换成数组
- 将字典或者数组转换成JSON数据或者字符串
- json对象、json格式字符串、java对象【使用此net.sf.json.JSONObject将java对象[比如数组集合Map、Map扩展]转换成json格式字符串】
- form表单转换为json格式,支持数组