在jQuery中获取Json数据

没有一个明确的例子可以解释如何尽可能简单地提取json数据。 我有一个有效的json,我需要使用jQuery检索它

我的json输出如下:

{ "title": "blog entries", "items" : [ { "title": "Can Members of the Diaspora Work Effectively at th", "date": "8/4/2009 9:42:38 AM" }, { "title": "Ashoka Brazil", "date": "7/15/2009 8:56:12 AM" }, { "title": "Life Spring Hospital", "date": "7/15/2009 8:56:12 AM" }, { "title": "Pozitron/Endeavor", "date": "5/26/2009 8:58:39 PM" } ] } 

我尝试用以下方法检索它,但没有运气。

  $.getJSON({ type: "GET", data: { PROCESS: "ViewBlog" }, url: "http://www.pangeaadvisors.org/sep123/blog.cs.asp", dataType: "json", success: function(json) { $(json).find('item').each(function(){ var title = $(this).find('title').text(); $('
').html(title).appendTo('#news_wrap'); }); } });

试试这个

 $.getJSON("http://www.pangeaadvisors.org/sep123/blog.cs.asp",{ PROCESS: "ViewBlog" }, function(json) { for (var i = 0; i < json.length; i++) { var title = json[i].Title; $('
').html(title).appendTo('#news_wrap'); } });

正如redsquare回答你需要或$ .each 🙂

UPDATE

由于你的url在url中有2个点,它失败了

假设请求正在运行(检查firebug以查看请求是否作为脚本标记发出并且响应返回)您将需要执行

 $.each( json.items, function(){ ... }); 

或者你可以使用普通的js

 for (var i=0; i 

您将不得不考虑如何使用ajax进行跨域调用。 浏览器会拒绝这样的请求。

试试这个

 var items = test.items; $.each(items,function(index,items){ console.log(items.title); /// and items.date })