我有这个JSON:
{
"fakultaeten": [
{
"id": "1",
"name": "Carl-Friedrich Gauß",
"institut": [
{
"Mathematik": [
{
"Verbrauch": "852 kWH",
"Effizienz": "5,5"
}
],
"Informatik": [
{
"Verbrauch": "852 kWH",
"Effizienz": "5,5"
}
],
"Wirtschaftswissenschaften": [
{
"Verbrauch": "852 kWH",
"Effizienz": "5,5"
}
],
"Sozialwissenschaften": [
{
"Verbrauch": "852 kWH",
"Effizienz": "5,5"
}
]
}
],
"verbrauch": "852 kWH"
}
]
}
我想在“institut”中创建所有项目的列表,如下所示:
我正在尝试这个:
$.each(data.fakultaeten, function(key,value)
{
var mother = "<li id='first'>"+value.name+"<ul>";
$.each(value.institut, function(key1, value1)
{
// create the list here
});
})
结果仅为:[object Object]
我的解决方案出了什么问题?
答案 0 :(得分:1)
您的问题是institut
是一个包含单个对象的数组,因此您可以使用institut[0]
引用该单个对象。
这将有效:
$.each(data.fakultaeten, function (key, value) {
var mother = "<li id='first'>" + value.name + "<ul>";
$.each(value.institut[0], function (key1, value1) {
alert(key1);
});
});
答案 1 :(得分:0)
试试这个:
$.each(data.fakultaeten, function (key, value) {
var mother = "<li id='first'>" + value.name + "<ul>";
$.each(value.institut[0], function (key1, value1) {
console.log(key1); // key1 gives you output that you want
});
});