jQuery JSON 返回 [object Object]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13456610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON returning [object Object]
提问by gschervish
I am trying to return the JSON data from the specified URL but when the alert pops up it just shows [object Object] (I realize the object object is not in fact an error). I would like to spit out the position name and other fields in the alert. How do I do this?
我试图从指定的 URL 返回 JSON 数据,但是当警报弹出时,它只显示 [object Object](我意识到 object 对象实际上不是错误)。我想吐出警报中的职位名称和其他字段。我该怎么做呢?
Here is an example of the JSON I am looking at (the full file has about 30 postings)
这是我正在查看的 JSON 示例(完整文件大约有 30 个帖子)
[
{
"m_id": 473644,
"m_positionName": "Application Monitoring Software Engineer",
"m_positionLocations": [
{}
],
"m_active": true,
"m_description": "Job Responsibilities:\r\n\r\n-Create world class application monitoring tools and dashboards for our health care applications\r\n\r\n-Develop business rules to pro actively identify and re-mediate system-level issues before they occur.\r\n\r\n-Create business intelligence reports for internal and external use as a supplement to software products.\r\n\r\n\r\n\r\nJob Requirements:\r\n\r\n-BS or MS Degree in computer science or any engineering discipline.\r\n-4+ years of experience with Java (or other object-oriented programming language).\r\n-Experience in SQL, Struts, Hibernate, Spring, Eclipse, JSP, JavaScript.\r\n-Highly motivated and self-driven personality.\r\n-Excellent interpersonal and leadership skills.\r\n-A vision for the future and a desire to make a difference.\r\n-Experience with Maven, Tomcat, PostgreSql, Jasper Reports,",
"m_postedDate": "Jun 29, 2012 9:17:19 AM",
"m_closingDate": "Jun 29, 2013 12:00:00 AM"
}
]
And here is the script I am using.
这是我正在使用的脚本。
$.ajax({
type: "GET",
url: '/wp-content/themes/twentyeleven/js/jobopenings.json',
async: false,
beforeSend: function(x) {
if(x && x.overrideMimeType) {
x.overrideMimeType("application/j-son;charset=UTF-8");
}
},
dataType: "json",
success: function(data){
alert(data);
}
});
Any Help is much appreciated.
任何帮助深表感谢。
采纳答案by eseceve
Try this:
尝试这个:
success: function(data)
{
var _len = data.length;
, post, i;
for (i = 0; i < _len; i++) {
//debugger
post = data[i];
alert("m_positionName is "+ post. m_positionName);
}
}
回答by William Byrne
You could always turn the object into a string and alert that.
你总是可以把对象变成一个字符串并提醒它。
alert(JSON.stringify(data));
回答by Magus
When jQuery receive a json, jQuery automatically convert it to a javascript object. So data
just contains your object ready to be used.
If you want to access to the original text of the response, you can do this :
当 jQuery 接收到 json 时,jQuery 会自动将其转换为 javascript 对象。所以data
只包含您准备使用的对象。如果要访问响应的原始文本,可以执行以下操作:
success: function(data, textStatus, jqXHR){
alert(jqXHR.responseText);
}