我正在浏览一堆 JSON 对象,首先创建一个“名称”列表,然后使用名称搜索 JSON 文件并从“名称”对象下方抓取各种对象。
我遇到的问题是,当 JSON 文件中有四个以上的对象时,脚本会严重崩溃(浏览器挂起,然后给我一个“警告:脚本无响应”错误)。
访问前四个对象很好,但是单击生成的链接转到第五个对象时,一切都会出错。
示例 JSON 如下...</p>
{"glass":[
{
"name":"Drink Name 1",
"ingredients":{
"liquids":["Gin", "Tonic"],
"amounts":[60, 180],
"colours":["ffffff", "51c98c"],
"garnish":["Cucumber slice"],
"ice":["cubes"]
},
"method":["Add plenty of ice into glass", "Pour gin over ice", "Top up with tonic", "Stir"],
"finished":["FAFFFE"]
},
{
"name":"Drink Name 2",
"ingredients":{
"liquids":["Gin", "Tonic"],
"amounts":[60, 180],
"colours":["ffffff", "51c98c"],
"garnish":["Cucumber slice"],
"ice":["cubes"]
},
"method":["Add plenty of ice into glass", "Pour gin over ice", "Top up with tonic", "Stir"],
"finished":["FAFFFE"]
},
{
"name":"Drink Name 3",
"ingredients":{
"liquids":["Gin", "Tonic"],
"amounts":[60, 180],
"colours":["ffffff", "51c98c"],
"garnish":["Cucumber slice"],
"ice":["cubes"]
},
"method":["Add plenty of ice into glass", "Pour gin over ice", "Top up with tonic", "Stir"],
"finished":["FAFFFE"]
},
{
"name":"Drink Name 4",
"ingredients":{
"liquids":["Gin", "Tonic"],
"amounts":[60, 180],
"colours":["ffffff", "51c98c"],
"garnish":["Cucumber slice"],
"ice":["cubes"]
},
"method":["Add plenty of ice into glass", "Pour gin over ice", "Top up with tonic", "Stir"],
"finished":["FAFFFE"]
},
{
"name":"Drink Name 5",
"ingredients":{
"liquids":["Gin", "Tonic"],
"amounts":[60, 180],
"colours":["ffffff", "51c98c"],
"garnish":["Cucumber slice"],
"ice":["cubes"]
},
"method":["Add plenty of ice into glass", "Pour gin over ice", "Top up with tonic", "Stir"],
"finished":["FAFFFE"]
}
]
}
我的代码(其中的一部分)目前看起来像这样……</p>
//Get the JSON list and search through it for a match to the recipe name
$.getJSON(url, function(data) {
cache:false;
for (var i=0;i<data.glass.length;i++){
var glass = data.glass[i]
//Matching the recipe to the cocktail name
if (glass.name == recipe){
// Saving the liquids array as a variable
liquidIng = (data.glass[i].ingredients.liquids)
console.log(liquidIng);
//Saving the amounts as a variable
liquidAmounts = (data.glass[i].ingredients.amounts)
var total = 0
for (var i = 0; i < liquidAmounts.length; i++) {
total += parseInt(liquidAmounts[i]);
}
//Divide the total by 100 to get onePercent
var onePercent
onePercent = total /100;
//Write and multiply the amounts by the onePercent
var outputAmounts="<div style=\"height:100%\">";
for (var i in liquidAmounts) {
outputAmounts+="<div style=\"height:" + liquidAmounts[i] / onePercent + "%; background-color:#" + glass.ingredients.colours[i] + "\">" + glass.ingredients.liquids[i] + "</div>";
}
outputAmounts+="</div>";
document.getElementById("ingredientsAmounts").innerHTML=outputAmounts;
//Write the list of ingredients
var outputIngredients="<ul>";
for (var i in liquidIng) {
outputIngredients+="<li>" + glass.ingredients.liquids[i] + "</li>";
}
outputIngredients+="</ul>";
outputIngredients+="<p>" + glass.ingredients.garnish[0] + "</p>";
document.getElementById("ingredientsList").innerHTML=outputIngredients;
//Writing the Method
var outputMethod="<ul>";
for (var i in glass.method) {
outputMethod+="<li>" + glass.method[i] + "</li>";
}
outputMethod+="</ul>";
document.getElementById("methodList").innerHTML=outputMethod;
}//Matching cocktail ends
}//Get Recipe function ends
});
该脚本会输出它应该输出的 HTML,但在它经过六秒钟的思考之前不会输出。然后一切都停止了。
我能理解的最接近调试消息的内容是显示为…</p>
var glass = data.glass[i]
我对 JQuery 很陌生,所以请尽量忽略我的无知——我真的很难过。任何帮助将不胜感激。(另外,为发布大量 JSON 代码道歉)
谢谢,马特