我已经到处寻找这个问题的答案,但我没有看到。
我正在使用的东西的一点历史。我正在从一台服务器加载 XML,将其转换为我服务器上的 JSON 文件,因此我可以轻松地使用它(使用 JS)。
我的 json 看起来像这样:
{ "listings": [{ "detailsLink": "http://idx.firstidx.com/NewDetails.aspx?MLS=2529220&TableType=SingleFamily&DatabaseID=50&Domain=639 ", "detailsPrice": "149000", "detailsBeds": "0", "detailsBaths": "1", "detailsCity": "Floral Park", "detailsImage": "http://3pv.mlsstratus.com/mlsmultiphotos/full/1/220/2529220.jpg", "detailsState": "NY" }]}
我的 jquery 看起来像这样:
$.getJSON('get-json.asp', function(data){
$.each(data.listings, function(i,l){
var detailsLink = l.detailsLink;
var detailsImage = l.detailsImage;
var detailsPrice = l.detailsPrice;
var detailsBeds = l.detailsBeds;
var detailsBaths = l.detailsBaths;
var detailsCity = l.detailsCity;
var detailsState = l.detailsState;
// Here is where I plan to do magical things with my new variables.
});
});
回调函数没有为我触发。当我从 JSON 文件中删除 detailsLink 时,它会毫无问题地触发。detailsImage 不会导致错误。
我通过在线免费服务检查了我的 JSON 是否有效,如果我手动粘贴 JSON,它会变得干净,但是当我从 URL 加载它时,它显示 detailsLink 具有无效字符。
我需要做些什么来为 JSON 输出准备我的 detailsLink (URL) 吗?
提前致谢。
这是我的 ASP 文件的 VB:
Set listings = xml.getElementsByTagName("PropertyDetails")
Response.Write "{"
Response.Write " ""listings"": ["
For i = 0 to (listings.Length-1)
Response.Write "{"
Response.Write " ""detailsPrice"":" & " """ & listings.item(i).childNodes(11).text & """, "
Response.Write " ""detailsLink"":" & " """ & listings.item(i).childNodes(0).text & """, "
Response.Write " ""detailsBeds"":" & " """ & listings.item(i).childNodes(8).text & """, "
Response.Write " ""detailsBaths"":" & " """ & listings.item(i).childNodes(9).text & """, "
Response.Write " ""detailsCity"":" & " """ & listings.item(i).childNodes(4).text & """, "
Response.Write " ""detailsImage"":" & " """ & listings.item(i).childNodes(15).text & """, "
Response.Write " ""detailsState"":" & " """ & listings.item(i).childNodes(6).text & """ "
if i < (listings.length-1) then 'If this is not the last listing add a comma after the curley brace
Response.Write "},"
else
Response.Write "}" 'This is the last listing, so no comma
end if
Next
Response.Write "]"
Response.Write "}"