I have wasted so much time on this..the recursion part is quite illusive.
for a given HTML structure, of unknown depth, I need to convert to JSON.
(I use this from some YAML i18n translation system I am building)
my general idea is to go deep until it finds the INPUT
, then create an
object with the key/value of the span.innerHTML/input.value
, and return that
object, so it will be the VALUE of a KEY that is the last <span class="title">
reached.
(Yes, it's a bit complicated but very interesting to develop)
JSBIN playground - live code example
I can't get my recursive function to work properly, to output the JSON I want...
HTML structure
<ul>
<li>
<span class="title">footer</span>
<ul>
<li>
<span>statement</span>
<input type="text" value="xxx">
</li>
</ul>
</li>
<li>
<span class="title">landing</span>
<ul>
<li>
<span>page_title</span>
<input type="text" value="yyy">
</li>
<li>
<span>page_sub_title</span>
<input type="text" value="xxx">
</li>
<li>
<span class="title">pricing</span>
<ul class="level11">
<li>
<span>title</span>
<input type="text" value="aaa">
</li>
<li>
<span>cost</span>
<input type="text" value="xxx">
</li>
</ul>
</li>
</ul>
</li>
</ul>
(Wanted) JSON output
{
footer : {
statement : 'xxx'
},
landing : {
page_title : 'yyy',
page_sub_title : 'xxx',
pricing : {
title : 'aaa',
cost : 'xxx'
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…