[英]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 + " ");
});
});
我所得到的是
[对象对象] [对象对象]
如何使数据正确显示?
假设:
您的循环应更像:
$.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); }); } }); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.