$.toJSON的用法或把数组转换成json类型
2015-03-11 17:00
190 查看
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;
}
}
}
相关文章推荐
- $.toJSON的用法或把数组转换成json类型
- $.toJSON的用法或把数组转换成json类型
- $.toJSON的用法或把数组转换成json类型(转)
- AJAX $.toJSON的用法或把数组转换成json类型
- AJAX $.toJSON的用法或把数组转换成json类型
- $.toJSON的用法或把数组转换成json…
- php数组转换js数组操作及json_encode的用法详解
- 整型-文件-方法名-日期-文件拷贝-缩略图-JSON创建-PDF-HTTP-单例-抓屏-目录-ZIP-XML-Map-邮件-Array-beanCopy-数组类型转换-数组集合转换-枚举示例
- String类型转换成数组,并Json形式提交
- json解析转化成数组,字典等类型,nsstring 转换
- js中常用数据类型之间转换--字符串转换成数字;----字符串和json;---字符串和数组
- 03_模板、类型转换、新数组、Lamdbda表达式、New高级用法
- String类型的json体转换为json(json嵌套字符串json,嵌套字符串json数组等)转换并解析为需要的json格式
- 通过Ajax技术,将数组类型数据转换为JSON格式的对象
- json数组与对象类型转换
- json数组如何转换成string类型(超级好用)
- c#中数组,类型转换,using语句的其他用法
- 图像数组转换及图像类型转换相关函数
- 时间类型的转换,SQL中CONVERT转化函数的用法 (转载)