Advertisement

将html table转为json

阅读量:

html table 转json

2018年1月,max.bai在开发基于Django的接口平台过程中,前端需要将表格数据转换为JSON格式,为此自行编写了一段代码,并对此过程进行了记录。

1. html table 设置

首先需对表格格式进行标准化处理

a. 表格结构中必须包含thead和th元素,以及tbody部分

b. 每个th标签都需要设置name属性,该属性的值将作为生成json数据时对应的键名

c. 在td单元格中,若需要获取控件的值,则应添加jsonval属性,无需指定具体值,脚本会自动识别并提取对应控件的值;若未设置jsonval属性,则默认将该字段的值设为空字符串

示例:

复制代码
 <table class="table" id="pcheck_items">

    
    <thead> 
    
     <tr> 
    
      <th name="check_key">Key</th> 
    
      <th name="check_module">Validation</th> 
    
      <th name="check_value">Expect</th> 
    
      <th name="action"></th> 
    
     </tr> 
    

全部评论 (0)

还没有任何评论哟~