我在将以下html代码转换为json时面临一个问题:
<div class="control" >
<label>Caption</label><br>
<select name="" class="dropdowntag">
<option value="Option1">Option1</option>
<option value="Option2">Option2</option>
</select>
</div>
在这个结果JSON中,缺少 select 标记,以下是我从json2html网站中提供的变换构建器获得的结果。
{"tag":"div","class":"control","children":[
{"tag":"label","html":"Caption"},
{"tag":"br","html":""}, [
{"tag":"option","value":"Option1","html":"Option1"},
{"tag":"option","value":"Option2","html":"Option2"}
]
]}
如果我删除了外部div,我正确地获得了json。以下是修改后的html及其json结果:
<label>Caption</label><br>
<select name="" class="dropdowntag">
<option value="Option1">Option1</option>
<option value="Option2">Option2</option>
</select>
[
{"tag":"label","html":"Caption"},
{"tag":"br","html":""},
{"tag":"select","class":"dropdowntag","name":"","children":[
{"tag":"option","value":"Option1","html":"Option1"},
{"tag":"option","value":"Option2","html":"Option2"}
]}
]
无法弄清楚这个问题的原因。
注意:如果我将select标记更改为其他有效的html标记,或者只是提供除select之外的任何其他名称,则可以正常工作。
以下是用于此的jquery和json2html插件的版本:
以下是json2html插件网站的链接: json2html 此页面中的“构建器”选项卡提供了基于html构建转换器的选项。
答案 0 :(得分:1)
正确的转换应该是
{"tag":"div","class":"control","children":[
{"tag":"label","html":"Caption"},
{"tag":"br","html":""},
{"tag":"select","class":"dropdowntag","name":"","children":[
{"tag":"option","value":"Option1","html":"Option1"},
{"tag":"option","value":"Option2","html":"Option2"}
]}
]}
我已经确认它无法在json2html.com上运行,看起来转换转换的DOM方法似乎并不像某些原因那样选择。感谢您接受这个并且我会调查它