帮助 jQuery 抓取 json 数据
我在这里遇到了一些麻烦,我的 json 数据输出如下:
{"date":[{"day_w":"Tuesday","day_n":"28","month":"Dec"}],"subscriptions":[{"subscribe":"example1"},{"subscribe":"example2"},{"subscribe":"example3"}]}
我正在使用 jQuery 代码:
$.getJSON("example.php",function(data){
$.each(data.subscriptions, function(i, item) {
var subscribeData = "<li>"+ item.subscribe +"</li>";
$('#list').append(subscribeData);
});
但我在获取日期数组时遇到问题。我不想使用 .each 因为只有一个数组保存日期。这有道理吗?有人可以帮忙吗?
I am having some trouble here, my json data is outputting as follows:
{"date":[{"day_w":"Tuesday","day_n":"28","month":"Dec"}],"subscriptions":[{"subscribe":"example1"},{"subscribe":"example2"},{"subscribe":"example3"}]}
I am using the jQuery code:
$.getJSON("example.php",function(data){
$.each(data.subscriptions, function(i, item) {
var subscribeData = "<li>"+ item.subscribe +"</li>";
$('#list').append(subscribeData);
});
but I am having an issue grabbing the date array. I don't want to have to use .each because there is only one array holding the date. Does this make sense? Can anyone please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么
date
是一个数组?为什么不直接把对象放在那里呢?如果这不是一个选项,您可以直接访问
date[0]
:Why is
date
an array at all? Why not just have the object in there directly?If that's not an option, you can just access
date[0]
:您可以编写
data.date[0]
来获取数组中的第一个对象。You can write
data.date[0]
to get the first object in the array.试试这个 - http://jsfiddle.net/FloydPink/bAtEW/
Try this - http://jsfiddle.net/FloydPink/bAtEW/