Javascript 是否将嵌套列表转换为嵌套对象?
下面的方法几乎有效,但如果我也删除了Javascript 是否将嵌套列表转换为嵌套对象?,javascript,jquery,Javascript,Jquery,下面的方法几乎有效,但如果我也删除了(请参见下面我要查找的输出结果) 我试图做的是生成一个我可以从嵌套列表中读取的对象,以便生成思维导图(D3) HTML <div id="tree"> <ul class="sortable"> <li><a href="http://google.com">flare</a> <ul class="sortable"> <li>analytics
(请参见下面我要查找的输出结果)
我试图做的是生成一个我可以从嵌套列表中读取的对象,以便生成思维导图(D3)
HTML
<div id="tree">
<ul class="sortable">
<li><a href="http://google.com">flare</a>
<ul class="sortable">
<li>analytics
<ul class="sortable">
<li><a href="http://google.com">cluster</a>
<ul class="sortable">
<li><a href="http://google.com">AgglomerativeCluster</a></li>
<li><a href="http://google.com">CommunityStructure</a></li>
<li><a href="http://google.com">HierarchicalCluster</a></li>
<li><a href="http://google.com">MergeEdge</a></li>
</ul>
</li>
<li><a href="http://google.com">graph</a>
<ul class="sortable">
<li><a href="http://google.com">BetweennessCentrality</a></li>
<li><a href="http://google.com">LinkDistance</a></li>
<li><a href="http://google.com">MaxFlowMinCut</a></li>
<li><a href="http://google.com">ShortestPaths</a></li>
<li><a href="http://google.com">SpanningTree</a></li>
</ul>
</li>
<li><a href="http://google.com">optimization</a>
<ul class="sortable">
<li><a href="http://google.com">AspectRatioBanker</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="http://google.com">Pippo</a></li>
<li><a href="http://google.com">buonanotte</a></li>
<li><a href="http://google.com">Avantieri</a></li>
</ul>
</div>
<div id="d" style="margin-top: 40px; padding-top: 20px;">Output:<br><br><pre></pre></div>
<div id="d" style="margin-top: 40px; padding-top: 20px;">Output:<br><br><pre></pre></div>
<div id="json_results" style="margin-top: 40px; padding-top: 20px;">
Needs to become:
<pre style="padding-top: 20px;">
var root = JSON.parse( ' {
"name": "flare",
"children": [
{
"name": "analytics",
"children": [
{
"name": "cluster",
"children": [
{"name": "AgglomerativeCluster","url":"http://google.com"},
{"name": "CommunityStructure"},
{"name": "HierarchicalCluster"},
{"name": "MergeEdge"}
]
},
{
"name": "graph",
"children": [
{"name": "BetweennessCentrality" },
{"name": "LinkDistance" },
{"name": "MaxFlowMinCut" },
{"name": "ShortestPaths" },
{"name": "SpanningTree" }
]
},
{
"name": "optimization",
"children": [
{"name": "AspectRatioBanker" }
]
}
]
},
{"name": "Pippo"},
{"name": "buonanotte" },
{"name": "Avantieri"}
]
} ') ;
</pre>
</div>
json应该是:
$(document).ready(function () {
var out = [];
function processOneLi(node) {
var aNode = node.children("a:first");
var retVal = {};
if (aNode.text()) {
retVal = {
"name": aNode.text(),
"url": aNode.attr("href")
};
} else {
retVal = { "name": node.clone().children().remove().end().text().replace(/\r?\n|\r/g, " ") };
}
node.find("> .sortable > li").each(function() {
if (!retVal.hasOwnProperty("children")) {
retVal.children = [];
}
retVal.children.push(processOneLi($(this)));
});
return retVal;
}
$("#tree > ul > li").each(function() {
out.push(processOneLi($(this)));
});
$('#d pre').text(JSON.stringify(out, null, 2));
});
但我得到的却是:
<div id="tree">
<ul class="sortable">
<li><a href="http://google.com">flare</a>
<ul class="sortable">
<li>analytics
<ul class="sortable">
<li><a href="http://google.com">cluster</a>
<ul class="sortable">
<li><a href="http://google.com">AgglomerativeCluster</a></li>
<li><a href="http://google.com">CommunityStructure</a></li>
<li><a href="http://google.com">HierarchicalCluster</a></li>
<li><a href="http://google.com">MergeEdge</a></li>
</ul>
</li>
<li><a href="http://google.com">graph</a>
<ul class="sortable">
<li><a href="http://google.com">BetweennessCentrality</a></li>
<li><a href="http://google.com">LinkDistance</a></li>
<li><a href="http://google.com">MaxFlowMinCut</a></li>
<li><a href="http://google.com">ShortestPaths</a></li>
<li><a href="http://google.com">SpanningTree</a></li>
</ul>
</li>
<li><a href="http://google.com">optimization</a>
<ul class="sortable">
<li><a href="http://google.com">AspectRatioBanker</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="http://google.com">Pippo</a></li>
<li><a href="http://google.com">buonanotte</a></li>
<li><a href="http://google.com">Avantieri</a></li>
</ul>
</li>
</ul>
</div>
<div id="d" style="margin-top: 40px; padding-top: 20px;">Output:<br><br><pre></pre></div>
<div id="json_results" style="margin-top: 40px; padding-top: 20px;">
Needs to become:
<pre style="padding-top: 20px;">
var root = JSON.parse( ' {
"name": "flare",
"children": [
{
"name": "analytics",
"children": [
{
"name": "cluster",
"children": [
{"name": "AgglomerativeCluster","url":"http://google.com"},
{"name": "CommunityStructure"},
{"name": "HierarchicalCluster"},
{"name": "MergeEdge"}
]
},
{
"name": "graph",
"children": [
{"name": "BetweennessCentrality" },
{"name": "LinkDistance" },
{"name": "MaxFlowMinCut" },
{"name": "ShortestPaths" },
{"name": "SpanningTree" }
]
},
{
"name": "optimization",
"children": [
{"name": "AspectRatioBanker" }
]
}
]
},
{"name": "Pippo"},
{"name": "buonanotte" },
{"name": "Avantieri"}
]
} ') ;
</pre>
</div>
这里是(请参见下面我要查找的输出结果)我发现了您的问题,有一个li元素没有您要查找的元素
这会让你得到你想要的结果
$(document).ready(function () {
var out = [];
function processOneLi(node) {
var aNode = node.children("a:first");
var retVal = {
"name": aNode.text(),
"url": aNode.attr("href")
};
node.find("> .sortable > li").each(function() {
if (!retVal.hasOwnProperty("children")) {
retVal.children = [];
}
retVal.children.push(processOneLi($(this)));
});
return retVal;
}
$("#tree > ul > li").each(function() {
out.push(processOneLi($(this)));
});
$('#d pre').text(JSON.stringify(out[0], null, 2));
});
您的问题是Pippo、buonanotte和Avantieri是flare的兄弟,而不是嵌套在flare下
我想我在这把小提琴上得到了你所期望的结果:
HTML:
一个小的递归函数可以:
函数列表对象(列表){
返回$(list).first().children(“li”).map(函数(){
var$this=$(this),$name=$this.children(“a”).first();
返回{
//看http://stackoverflow.com/a/8851526
名称:$name.text()| |$this.clone().children().remove().end().text().trim(),
url:$name.attr(“href”),
子对象:listToObject($this.children(“ul”))
};
}).toArray();
}
var输出=listToObject($(“#tree>ul”);
控制台日志(输出)代码>
-
- 分析
-
-
-
让它加载。我等待了几秒钟才得到相同的结果。不知道,我等待了,但它仍然是空的输出[]我现在得到了你的结果,请检查我的答案的更新。几乎,但我必须承认,这一个是有效的,但当我将json输出粘贴到其中时,它就不起作用了works@rob.mJSON字符串中有很多空格,因此很难解析。我为你更新了链接。现在有了更好的json字符串格式。为了澄清您的问题,为什么节点是空的?@DaniP不仅如此,基本上我得到的输出与我期望的不同,请参阅2 json以了解差异,是的,如果我删除了一个链接标签,最好是你准确地指出区别所在,而不是让我们逐行比较两个JSON,你不希望所有项目都使用url:
?。。。顺便说一句,我通过空名称@DaniP解决了这个问题。很抱歉,我认为提供尽可能多的细节会有所帮助。我们也有空对象的问题。如果节点没有提供url,请检查您的JSFIDLE输出小心,我们对第一个节点名称“name”有问题:“FlarelepipPobuonanotteavantieri”,
$(document).ready(function () {
var out = [];
function processOneLi(node) {
var aNode = node.children("a:first");
var retVal = {};
if (aNode.text()) {
retVal = {
"name": aNode.text(),
"url": aNode.attr("href")
};
} else {
retVal = { "name": node.clone().children().remove().end().text().replace(/\r?\n|\r/g, " ") };
}
node.find("> .sortable > li").each(function() {
if (!retVal.hasOwnProperty("children")) {
retVal.children = [];
}
retVal.children.push(processOneLi($(this)));
});
return retVal;
}
$("#tree > ul > li").each(function() {
out.push(processOneLi($(this)));
});
$('#d pre').text(JSON.stringify(out, null, 2));
});
<div id="tree">
<ul class="sortable">
<li><a href="http://google.com">flare</a>
<ul class="sortable">
<li>analytics
<ul class="sortable">
<li><a href="http://google.com">cluster</a>
<ul class="sortable">
<li><a href="http://google.com">AgglomerativeCluster</a></li>
<li><a href="http://google.com">CommunityStructure</a></li>
<li><a href="http://google.com">HierarchicalCluster</a></li>
<li><a href="http://google.com">MergeEdge</a></li>
</ul>
</li>
<li><a href="http://google.com">graph</a>
<ul class="sortable">
<li><a href="http://google.com">BetweennessCentrality</a></li>
<li><a href="http://google.com">LinkDistance</a></li>
<li><a href="http://google.com">MaxFlowMinCut</a></li>
<li><a href="http://google.com">ShortestPaths</a></li>
<li><a href="http://google.com">SpanningTree</a></li>
</ul>
</li>
<li><a href="http://google.com">optimization</a>
<ul class="sortable">
<li><a href="http://google.com">AspectRatioBanker</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="http://google.com">Pippo</a></li>
<li><a href="http://google.com">buonanotte</a></li>
<li><a href="http://google.com">Avantieri</a></li>
</ul>
</li>
</ul>
</div>
<div id="d" style="margin-top: 40px; padding-top: 20px;">Output:<br><br><pre></pre></div>
<div id="json_results" style="margin-top: 40px; padding-top: 20px;">
Needs to become:
<pre style="padding-top: 20px;">
var root = JSON.parse( ' {
"name": "flare",
"children": [
{
"name": "analytics",
"children": [
{
"name": "cluster",
"children": [
{"name": "AgglomerativeCluster","url":"http://google.com"},
{"name": "CommunityStructure"},
{"name": "HierarchicalCluster"},
{"name": "MergeEdge"}
]
},
{
"name": "graph",
"children": [
{"name": "BetweennessCentrality" },
{"name": "LinkDistance" },
{"name": "MaxFlowMinCut" },
{"name": "ShortestPaths" },
{"name": "SpanningTree" }
]
},
{
"name": "optimization",
"children": [
{"name": "AspectRatioBanker" }
]
}
]
},
{"name": "Pippo"},
{"name": "buonanotte" },
{"name": "Avantieri"}
]
} ') ;
</pre>
</div>
$(document).ready(function () {
var out = [];
function processOneLi(node) {
var aNode = node.children("a:first");
var retVal = {
"name": aNode.text(),
"url": aNode.attr("href")
};
node.find("> .sortable > li").each(function() {
if (!retVal.hasOwnProperty("children")) {
retVal.children = [];
}
retVal.children.push(processOneLi($(this)));
});
return retVal;
}
$("#tree > ul > li").each(function() {
out.push(processOneLi($(this)));
});
$('#d pre').text(JSON.stringify(out[0], null, 2));
});