8

我正在尝试从指定的 URL 返回 JSON 数据,但是当弹出警报时它只显示 [object Object] (我意识到 object 对象实际上不是错误)。我想在警报中吐出职位名称和其他字段。我该怎么做呢?

这是我正在查看的 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"
  }
]

这是我正在使用的脚本。

 $.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);
}
});

任何帮助深表感谢。

4

3 回答 3

19

你总是可以把对象变成一个字符串并提醒它。

alert(JSON.stringify(data));
于 2012-11-19T15:15:16.723 回答
7

尝试这个:

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);
  }
}
于 2012-11-19T15:24:33.273 回答
1

当 jQuery 接收到一个 json 时,jQuery 会自动将其转换为 javascript 对象。所以data只包含准备好使用的对象。如果要访问响应的原始文本,可以这样做:

success: function(data, textStatus, jqXHR){
    alert(jqXHR.responseText);
}
于 2012-11-19T15:19:02.427 回答