Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Jquery中将html表转换为Json_Jquery_Json - Fatal编程技术网

在Jquery中将html表转换为Json

在Jquery中将html表转换为Json,jquery,json,Jquery,Json,我有一张桌子,每个tr都有课。我希望将其转换为Json,以便在服务器端使用。 表内容如下: <table class="tableContent"> <thead> <tr class="header"> <th class="col1">RoleName</th> <th class="col2">ReadOnly</th> <th class="c

我有一张桌子,每个tr都有课。我希望将其转换为Json,以便在服务器端使用。 表内容如下:

<table class="tableContent">
<thead>
    <tr class="header">
        <th class="col1">RoleName</th>
        <th class="col2">ReadOnly</th>
        <th class="col3">Modify</th>        
        <th class="col4">Approve</th>
    </tr>
</thead>
<tbody> 
    <tr class="1">
        <td>RoleA</td>
        <td><input type="checkbox" class="readOnly"></td>
        <td><input type="checkbox" class="modify" checked></td>
        <td><input type="checkbox" class="approve"></td>        
    </tr>
    <tr class="1">
        <td>RoleB</td>
        <td><input type="checkbox" class="readOnly"></td>
        <td><input type="checkbox" class="modify" checked></td>
        <td><input type="checkbox" class="approve"></td>        
    </tr>
    <tr class="2">
        <td>RoleA</td>
        <td><input type="checkbox" class="readOnly" checked></td>
        <td><input type="checkbox" class="modify" checked></td>
        <td><input type="checkbox" class="approve"></td>        
    </tr>
    <tr class="2">
        <td>RoleB</td>
        <td><input type="checkbox" class="readOnly" checked></td>
        <td><input type="checkbox" class="modify" checked></td>
        <td><input type="checkbox" class="approve" checked></td>        
    </tr>
</tbody>
</table>
在这里,class属性值是json中的字段id,用户可以使用多个角色

任何有助于解决问题的指导都将不胜感激

更新:为了创建如上所述的列表,添加了一个额外的列,然后以相同的格式创建Json。 正在工作的JS小提琴:

var myRows=[];
var$headers=$(“th”);
var$rows=$(“tbody tr”)。每个(函数(索引){
$cells=$(this.find(“td”);
myRows[index]={};
$cells.each(函数(cellIndex){
myRows[index][$($headers[cellIndex]).html()]=$(this.html();
});    
});
var myObj={};
myObj.myrows=myrows;
警报(JSON.stringify(myObj))

罗兰胺
只读
修改
批准
罗莱亚
罗勒布
罗莱亚
罗勒布
var myRows=[];
var$headers=$(“th”);
var$rows=$(“tbody tr”)。每个(函数(索引){
$cells=$(this.find(“td”);
myRows[index]={};
$cells.each(函数(cellIndex){
myRows[index][$($headers[cellIndex]).html()]=$(this.html();
});    
});
var myObj={};
myObj.myrows=myrows;
警报(JSON.stringify(myObj))

罗兰胺
只读
修改
批准
罗莱亚
罗勒布
罗莱亚
罗勒布

的可能重复项。使用工作代码更新的可能重复项。使用工作代码更新的可能重复项上面没有给出所需格式的json,如果您检查输出,也会包含输入类型,但是我想排除它,而是检查它是否被选中,然后将值设置为true或false。我已经更新了您的代码并从复选框中获取了输入。JS小提琴手:让我工作。谢谢:)上面没有给出所需格式的json,如果您检查输出,也会包含输入类型,但是我想排除它,而是检查它是否被选中,然后将值设置为true或false。我已经更新了您的代码并从复选框中获取输入。JS小提琴手:让我工作。谢谢:)
[{"id":1,
"roles":[
  {
    "name": "RoleA",
    "readOnly": false,
    "modify": true,
    "approve": false
  },
  {
    "name": "RoleB",
    "readOnly": false,
    "modify": true,
    "approve": true
  }
]},
{"id":2,
"roles":[
  {
    "name": "RoleA",
    "readOnly": true,
    "modify": true,
    "approve": false
  },
  {
    "name": "RoleB",
    "readOnly": true,
    "modify": true,
    "approve": true
  }
]}]