Javascript JSON文件中的嵌套列表内容
我有一个嵌套的无序列表,我想从JSON文件中动态获取内容,而不是用HTML编写。为此,我知道我需要一些脚本,但我并不确切知道 我在用HTML和JSON文件构建了当前列表,只是缺少脚本 HTML…Javascript JSON文件中的嵌套列表内容,javascript,jquery,json,html,Javascript,Jquery,Json,Html,我有一个嵌套的无序列表,我想从JSON文件中动态获取内容,而不是用HTML编写。为此,我知道我需要一些脚本,但我并不确切知道 我在用HTML和JSON文件构建了当前列表,只是缺少脚本 HTML… 谢谢你帮助我 您只需要循环。建立一个字符串。循环的基本思想 var states = yourJSON.states; states.forEach( function (state) { console.log("\t" + state.name); var cities = state.
谢谢你帮助我 您只需要循环。建立一个字符串。循环的基本思想
var states = yourJSON.states;
states.forEach( function (state) {
console.log("\t" + state.name);
var cities = state.city;
cities.forEach( function (city) {
console.log("\t\t" + city.name);
var stores = city.stores;
stores.forEach( function (store) {
console.log("\t\t\t" + store.name);
});
});
});
你只需要循环一下。建立一个字符串。循环的基本思想
var states = yourJSON.states;
states.forEach( function (state) {
console.log("\t" + state.name);
var cities = state.city;
cities.forEach( function (city) {
console.log("\t\t" + city.name);
var stores = city.stores;
stores.forEach( function (store) {
console.log("\t\t\t" + store.name);
});
});
});
在plunker中,您提到json不是有效的json格式。首先解决这些JSON错误,然后尝试以下代码:
var states = json.states;
var tempHtml = '<ul>';
for(state in states){
tempHtml += "<li><p>"+state.name+"</p><ul>";
for(city in state.city){
tempHtml += "<li><p>"+city.name+"</p><ul>";
for(store in city.stores){
tempHtml += "<li><h1>"+store.name+"</h1><p>"+store.address+"</p><a href='"+store.map+"'>view map</a></li>";
}
tempHtml += "</ul></li>";
}
tempHtml += "</ul></li>";
}
tempHtml += "</ul>";
$('#divid').html(tempHtml);
var states=json.states;
var tempHtml=“”;
for(州中的州){
tempHtml+=“- ”+state.name+”
”;
for(州内城市。城市){
tempHtml+=“- ”+city.name+”
”;
用于(城市中的商店。商店){
tempHtml+=“- ”+store.name+”“+store.address+”
”;
}
tempHtml+=“
”;
}
tempHtml+=“
”;
}
tempHtml+=“
”;
$('#divid').html(tempHtml);
在plunker中,您提到json不是有效的json格式。首先解决这些JSON错误,然后尝试以下代码:
var states = json.states;
var tempHtml = '<ul>';
for(state in states){
tempHtml += "<li><p>"+state.name+"</p><ul>";
for(city in state.city){
tempHtml += "<li><p>"+city.name+"</p><ul>";
for(store in city.stores){
tempHtml += "<li><h1>"+store.name+"</h1><p>"+store.address+"</p><a href='"+store.map+"'>view map</a></li>";
}
tempHtml += "</ul></li>";
}
tempHtml += "</ul></li>";
}
tempHtml += "</ul>";
$('#divid').html(tempHtml);
var states=json.states;
var tempHtml=“”;
for(州中的州){
tempHtml+=“- ”+state.name+”
”;
for(州内城市。城市){
tempHtml+=“- ”+city.name+”
”;
用于(城市中的商店。商店){
tempHtml+=“- ”+store.name+”“+store.address+”
”;
}
tempHtml+=“
”;
}
tempHtml+=“
”;
}
tempHtml+=“
”;
$('#divid').html(tempHtml);
这只是几个循环,我知道@epascarello!但我不知道怎么做;(但是,在你寻求帮助之前,你必须检查你提供的代码是否正确。你给出的Json格式不正确。你可以去w3school学习dom操作,包括创建元素等等。谢谢你的警告,@JoseMar!我认为这是正确的。我知道。这只是几个循环,我知道@epascarlo!但我不知道。)如何做到这一点;(但是,在你寻求帮助之前,你必须检查你提供的代码是否正确。你提供的Json格式不正确。你可以去w3school学习dom操作,包括创建元素等。谢谢你的警告,@JoseMar!我认为这是正确的。我知道。谢谢你的帮助@Aravinder!我已经和你一起消灭了这个劫掠者。)r脚本并修复了json错误,但仍有问题:(我认为它在plunker中起作用。您现在正在分阶段解决什么问题?我看到了一个与查看地图链接和更新上述脚本有关的问题。我已在plunker中修复了@aravinder。感谢您的帮助@aravinder!我已用您的脚本中断了plunker并修复了json错误,但仍有一些问题:(我认为它在plunker中起作用。您现在正在分阶段解决什么问题?我看到了一个与查看地图链接和更新上述脚本有关的问题。我已经在plunker中修复了@aravinder。谢谢您的帮助。