asp.net和Jquery中详细解析json数据格式!(有实例)

JSON格式:
比如学生有学号,姓名,性别等。
用json表示则为:

{"studno":"11111","studname":"wwww","studsex":"男"}(各个字段都是字符型)
这代表一个学生的信息。
如果多个呢?
[{"studno":"122222","studname":"wwww","studsex":"男"},
{"studno":"11111","studname":"xxxx","studsex":"男"},
{"studno":"33333","studname":"ssss","studsex":"男"}]
这就是json格式。
======================================================================================

新建一个 一般处理程序文件 Handler1.ashx
然后在Handler1.ashx.cs 里面写如下代码  public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string json = "[{'user_id':'123'}]";
            context.Response.Write(json);
        }
前台 用ajax 访问 Handler1.ashx 就可以得到json 数据了
建议 用jquery 的ajax 方法
$.ajax({
   type: "POST",
   url: "Handler1.ashx",
   data: "name=John&location=Boston",
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
});
=========================================================== asp.net中Jquery解析json数据格式

aspx前台页面代码如下:

<script language="javascript" type="text/javascript">
function TestAjax(){
$.ajax({
url: "www.2cto.com/", //ajax请求的路径
dataType: "json",
data: "id=150"+"&r=" + Math.random(), //请求的参数
type: "post",
success: function (data) {
alert(data.name); //弹出data对象的name属性值
}
});
}
</script>

后台.cs文件如下:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["id"] != null)
{
string id = Request.QueryString["id"].ToString();
Context.Response.ContentType = "text/plain";
//构建的json数据(只有一条数据可以直接用{},如果有多条数据需要用[],前台用索引访问) 例如jsonstr = "[{\"pricelist\":\"" + temm + "\",\"jianye\":\"" + jianye + "\",\"sigleprice\":\"" + oneytempprice + "\",\"jianyetotalprice\":\"" + jianyetotalprice + "\"}]";
string data = "{\"id\":\"" + id + "\",\"name\":\"测试内容 \",\"sigleprice\":\"120.0 \",\"jianyetotalprice\":\"123.2\"}";
Context.Response.Write(data);
Context.Response.End();
}
}
}

=========================================================== asp.net 集合传json数据格式
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Script.Serialization; //一定要引用的空间
/// <summary>
///JOSN 的摘要说明
/// </summary>
public class JOSN
{
public JOSN()
{

}
public string Tojson(object obj) {
JavaScriptSerializer serillizer = new JavaScriptSerializer();
return serillizer.Serialize(obj);
}
public string Tojson(object obj, int recursiondepth)
{
JavaScriptSerializer serialize= new JavaScriptSerializer();
serialize.RecursionLimit = recursiondepth;
return serialize.Serialize(obj);
}
}


下面是使用方法


JOSN josn = new JOSN(); //先实倒化


public string GetJosn() {
List<Class1> list = new List<Class1>();
Class1 c = new Class1();
c.ID = "0";
c.Value = "weping";
list.Add(c);


c = new Class1();
c.ID = "1";
c.Value = "weping1";
list.Add(c);


c = new Class1();
c.ID = "2";
c.Value = "weping2";
list.Add(c);


c = new Class1();
c.ID = "3";
c.Value = "weping3";
list.Add(c);


c = new Class1();
c.ID = "4";
c.Value = "weping4";
list.Add(c);


c = new Class1();
c.ID = "5";
c.Value = "weping5";
list.Add(c);


c = new Class1();
c.ID = "6";
c.Value = "weping6";
list.Add(c);


c = new Class1();
c.ID = "7";
c.Value = "weping7";
list.Add(c);


return josn.Tojson(list);//这样就返回json数据了
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值