getJSON警报未返回任何内容-数据问题
getJSON alert not returning anything - data issue?
我在读取以下数据时遇到问题:
它看起来像这样:
{
"ok": true,
"messages": [
{
"text": "moo",
"username": "bot",
"type": "message",
"subtype": "bot_message",
"ts": "1448226157.000008"
},
{
"text": "boo",
"username": "bot",
"type": "message",
"subtype": "bot_message",
"ts": "1448225998.000007"
}
],
"has_more": true
}
这是代码:
$.getJSON("https://slack.com/api/channels.history?token=xxxx&channel=C0E&pretty=1", function(result){
$.each(result, function(i, field){
$("div").append(field + " ");
});
});
我得到的只是
[对象对象][对象对象]
如何使数据正确显示?
假设:
- 这是完整的JSON
- 你想循环浏览消息
你的循环应该更像:
$.each( result.messages,
function( i, msg ) {
$("div").append( msg.username + ": " + msg.text + " " );
}
);
(或您希望显示的任何字段)
你的意思是这样的:
var data = `{"ok": true,"messages": [
{
"text": "moo",
"username": "bot",
"type": "message",
"subtype": "bot_message",
"ts": "1448226157.000008"
},
{
"text": "boo",
"username": "bot",
"type": "message",
"subtype": "bot_message",
"ts": "1448225998.000007"
}
],
"has_more": true
}`;
$(function() {
var result = $.parseJSON(data);
$.each(result, function(i, item) {
if (typeof item != "object") {
alert(item);
} else {
$.each(item, function(i, obj) {
alert(obj.ts);
});
}
});
});
样品小提琴:https://jsfiddle.net/4v0x34b1/
相关文章:
- jQuery 和 Ajax 请求数据问题
- JSON Ajax 数据问题
- 请求.JS和节点.js数据问题
- EasyXDM传递数据问题
- Symfony2:Highchartsbundle数据问题
- Google Analtyics Embedded API - report.数据问题
- D3 JS - 未捕获的类型错误:无法读取未定义的属性“长度” - 似乎与数据问题有关
- 提前输入获取远程数据问题
- 异步加载数据问题 AngularJS 和 Firebase
- 提前输入.js远程发送之前发送帖子数据问题
- 在 HTML 结构中打印 JSON 数据问题
- Morris Donut图表没有数据问题
- 如何修复此本地数据问题?(Chrome扩展、Javascript、jQuery)
- 使用Spring MVC控制器在JQuery Datatable中呈现数据问题
- getJSON警报未返回任何内容-数据问题
- 渲染后挖空JS从视图模型中检索数据问题
- b4jjquery-websockets库发送数据问题
- Javascript数据表填充数据问题(没有返回错误)
- Angular数据问题——不知道如何排除
- 奇怪的jQuery数据()问题-帮助