这可能是重复的,但我还没有找到我的问题的解决方案,即使我已经检查了许多JSON递归traversion功能的其他示例。迭代通过嵌套的数字键json对象
我的JSON OBJ看起来有点像下面这样:
function recurTrav (jsonObj) {
$.each(jsonObj.description[0], function (key, value) {
$(".testul").append("<li class=" + key + ">" + value + "</li>");
if (typeof(jsonObj[key] == "object")) {
recurTrav(jsonObj[key]);
}
});
}
recurTrav(obj);
给我什么:
var obj =
{
"description": [
{
"list": [
{
"1": "here is text"
},
{
"2": "other text"
},
{
"3": "arbitrary text"
},
{
"4": [
{
"1": "indented"
},
{
"2": {
"1": "indented to second level"
}
},
{
"3": "first indentation level again"
},
{
"4": {
"1": "second level again"
}
},
{
"5": "and first level, to wrap things up"
}
]
}
]
}
]
};
的东西,如穿越这一点。 (请注意,这是只是为了测试我怎么会遍历。我卡住了,这是令人尴尬的。
我想我会只需要在正确的方向一推......
我其实是什么希望做的是创造成一个无序列表结构。凡有可能成为主要的UL内ULS这一点。
<ul>
<li>here is text</li>
<li>other text</li>
<li>arbitrary text</li>
<li>
<ul>
<li>indented</li>
<li>
<ul>
<li>indented to second level</li>
</ul>
</li>
<li>first indentation level again</li>
<li>
<ul>
<li>second level again</li>
</ul>
</li>
<li>and first level, to wrap things up</li>
</ul>
</li>
</ul>
什么是你traversion规则?现在甚至没有像'for'循环那样的迭代,因为你的'jsonObj [key]'不会产生包含'.description'列表的东西 – twil
我不确定这是否是唯一的问题,但是括号会混淆在你的'如果'检查。它应该是'if(typeof(jsonObj [key])==“object”)' –
@PatrickQ是的,对不起,我输入的代码很快且很脏,可以确认这不是实际的问题。它产生了一堆不穿过的对象。 – NicT