【发布时间】:2012-12-16 17:02:30
【问题描述】:
我正在尝试解析这个 JSON 字符串:
{ "RESULTS": [ { "name": "Thessaloniki GR", "type": "Sailing", "l": "/sailing-weather/beach:Porto%20Carras%20Marina 45904" }, { "name": "Thessaloniki, Greece", "type": "city", "c": "GR", "zmw": "00000.1.16622", "tz": "Europe/Athens", "tzs": "EET", "l": "/q/zmw:00000.1.16622" } ] }
从here检索
这是我的sn-p:
$(document).ready(function () {
$("#w11").autocomplete({
source: function (a, b) {
$.ajax({
url: "http://autocomplete.wunderground.com/aq",
dataType: "jsonp",
data: {
format: "jsonp",
query: a.term
},
success: function (a) {
for (i in data.RESULTS) {
console.log(data.RESULTS);
}
}
})
}
});
});
这在第一行给我一个错误Uncaught SyntaxError: Unexpected token : { "RESULTS": [
如何解析 JSON 结果?
【问题讨论】:
-
你的 jquery 代码有错误
-
您的代码有一些问题,但不是问题所在。问题是 1. 您缺少几个分号。我强烈建议不要依赖 automatic semicolon insertion 的白痴。 2. 你似乎成为了The Horror of Implicit Globals 的牺牲品,因为你没有在任何地方声明
i。 3. 您的循环通过console.log(data.RESULTS);重复记录您的整个响应。你可能是指console.log(data.RESULTS[i]);或其他什么。 -
并且
success: function (a)中的a应该是data。