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前台页面代码如下:

后台.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; //一定要引用的空间
///
///JOSN 的摘要说明
///

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 list = new List();
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数据了
}

你可能感兴趣的:(asp.net和Jquery中详细解析json数据格式!(有实例))