Jquery getJSON方法分析

转自:http://www.cnblogs.com/jams742003/archive/2009/12/25/1632276.html

准备工作

·Customer类

public class Customer {

public int Unid { get; set; }

public string CustomerName { get; set; }

public string Memo { get; set; }

public string Other { get; set; }

}

·服务端处理(Json_1.ashx)

Customer customer = new Customer

{

  Unid=1,CustomerName="宋江",Memo="天魁星",Other="黑三郎"

};

string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(customer);
context.Response.Write(strJson);

(一)Jquery. getJSON

方法定义:jQuery.getJSON( url, data, callback )

通过get请求得到json数据

·url用于提供json数据的地址页

·data(Optional)用于传送到服务器的键值对

·callback(Optional)回调函数,json数据请求成功后的处理函数

function(data, textStatus) {

// data是一个json对象

// textStatus will be "success"

this; // the options for this ajax request

}

(1)一个对象

$.getJSON(     "webdata/Json_1.ashx",     function(data) {

$("#divmessage").text(data.CustomerName);

} );

向Json_1.ashx地址请求json数据,接收到数据后,在function中处理data数据。 这里的data的数据是一条记录,对应于一个customer实例,其中的数据以k/v形式存在。即以[object,object]数组形式存在。

{"Unid":1,"CustomerName":"宋江","Memo":"天魁星","Other":"黑三郎"}

所以在访问时,以data.Property来访问,下面以k/v循环来打印这条宋江的记录:

$.getJSON(     "webdata/Json_1.ashx",     function(data) {

  var tt="";

  $.each(data, function(k, v) {

    tt += k + ":" + v + "<br/>";

  })

$("#divmessage").html(tt);

});

结果:

Unid:1 CustomerName:宋江 Memo:天魁星 Other:黑三郎

(2)对象数组

Ashx文件(Json_1.ashx)修改:

List<Customer> _list = new List<Customer>();

Customer customer = new Customer { Unid=1,CustomerName="宋江",Memo="天魁星",Other="黑三郎"};

Customer customer2 = new Customer { Unid = 2, CustomerName = "吴用", Memo = "天机星", Other = "智多星" };
_list.Add(customer);

_list.Add(customer2);

string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(_list);

它生成的json对象的字符串是:

[{"Unid":1,"CustomerName":"宋江","Memo":"天魁星","Other":"黑三郎"},

{"Unid":2,"CustomerName":"吴用","Memo":"天机星","Other":"智多星"}]

这里可以看到做为集合的json对象不是再一条记录,而是2条记录,是一个[[object,object]]数组:[object,object][object,object],而每个[object,object]表示一条记录,对应一个Customer,其实也是k/v的形式,而这个v就是一个Customer对象,而这个k是从0开始的索引。

$.getJSON(     "webdata/Json_1.ashx",     function(data) {

   $.each(data, function(k, v) {

    alert(k);

  });

});

这时,k值为0,1……

列表json对象的方法:

$.getJSON(     "webdata/Json_1.ashx",     function(data) {

  var tt = "";

  $.each(data, function(k, v) {

     $.each(v,function(kk, vv) {

        tt += kk + ":" + vv + "<br/>";

     });

  });

$("#divmessage").html(tt);

});

结果:

Unid:1 CustomerName:宋江 Memo:天魁星 Other:黑三郎 Unid:2 CustomerName:吴用 Memo:天机星 Other:智多星

这里用了嵌套循环,第一个循环用于从List中遍历Customer对象,第二个循环用于从Customer对象中遍历Customer对象的属性,也就是k/v对。

关于序列化与反序列化请见其它随笔(JSON)

博客园大道至简

http://www.cnblogs.com/jams742003/

转载请注明:博客园

时间: 2024-07-29 20:25:09

Jquery getJSON方法分析的相关文章

Jquery getJSON方法分析(一)

准备工作 ·Customer类 public class Customer{    public int Unid { get; set; }    public string CustomerName { get; set; }    public string Memo { get; set; }    public string Other { get; set; }} ·服务端处理(Json_1.ashx) Customer customer = new Customer       {

jQuery源码中的Ajax--getScript()/getJson()方法

一.$.getScript()方法 有时候,在页面初次加载时就取得所需的全部Javascript文件是完全没必要的,可以按需所取. 该函数用于动态加载JS文件,并在全局作用域下执行文件中的JS代码. 该函数可以加载跨域的JS文件.请注意,该函数是通过异步方式加载数据的. 该函数属于全局jQuery对象. 语法: $(function(){ $("send").on("click",function(){ $.getScript("script.js&quo

jquery之getJSON方法获取中文数据乱码解决方法

最近公司做的东西要用到js,感觉js太繁琐,所以自己学起了jquery,发现jquery确实强大.在学到jquery ajax的时候(用的工具是eclipse),发现$.getJSON()方法请求服务器的json数据(有中文),返回到浏览器页面是乱码,怎么办呢? 原因是浏览器编码和服务器里的json数据编码不一致, 1.对着项目右键,properties,将text file encoding,改为utf-8.这时访问页面,发现还是乱码.我们还需要一步. 2.对着json文件,右键,proper

JQuery实现click事件绑定与触发方法分析

原生JS通过什么方法绑定click事件? 原生js有一下三种方法为DOM对象绑定click事件, 第一种,在html中添加 onclick属性,在此属性中添加要绑定的事件函数,如下, 这种方法为html处理事件的原始方法,使得html和js过分耦合, 即表现层代码 和 行为层代码耦合: <html> <head> <script src="./jquery.js"></script> </head> <body>

jQuery的get()post()getJson()方法

jQuery get() 和 post() 方法用于通过 HTTP GET 或 POST 请求从服务器请求数据. HTTP 请求:GET vs. POST 两种在客户端和服务器端进行请求-响应的常用方法是:GET 和 POST. GET - 从指定的资源请求数据 POST - 向指定的资源提交要处理的数据 GET 基本上用于从服务器获得(取回)数据.注释:GET 方法可能返回缓存数据. POST 也可用于从服务器获取数据.不过,POST 方法不会缓存数据,并且常用于连同请求一起发送数据. 1.j

对jQuery.extend()方法的分析

jQuery.extend方法是我们常用的方法,也是jQuery源码中的基础方法.它的主要作用是:将一个或多个“源对象”合并到一个“目标对象”中,并返回目标对象.它主要有三种表现形式: a.jQuery.extend(destination, source1, source2, source3 ....) b.jQuery.extend( source ) c.jQuery.extend(boolean, destination, source1, source2, source3 ....)

对jQuery.isArray方法的分析

jQuery.isArray方法应于判断是不是数组,是的话返回true,否则返回false.调用如:jQuery.isArray([]),返回true.其实现源码如下: isArray: Array.isArray || function( obj ) { return jQuery.type(obj) === "array"; } 我们看到它的主要逻辑是先看浏览器支不支持Array.isArray方法,如果不支持则调应jQuery自己的jQuery.type方法,看其返回值是不是&q

jquery源码分析(二)——结构

再来复习下整体架构: jQuery源码分析(基于 jQuery 1.11 版本,共计8829行源码) (21,94)                定义了一些变量和函数jQuery=function(){} (96,280)        给jQuery添加一些方法和属性,jQuery.fn=jQuery.prototype(285,347)        extend:        jQuery的一些继承方法        更容易进行后续的扩展                       

jQuery AJAX 方法

jQuery AJAX 方法 AJAX 是一种与服务器交换数据的技术,可以在补充在整个页面的情况下更新网页的一部分. 下面的表格列出了所有的 jQuery AJAX 方法: 方法 描述 $.ajax() 执行异步 AJAX 请求 $.ajaxPrefilter() 在每个请求发送之前且被 $.ajax() 处理之前,处理自定义 Ajax 选项或修改已存在选项 $.ajaxSetup() 为将来的 AJAX 请求设置默认值 $.ajaxTransport() 创建处理 Ajax 数据实际传送的对象