把html转换为json数据,HTML转成JSON数据
例如以下HTML
01010004一次性注射器10ml要轉換成 {"ID":"01010004","Name":"一次性注射器","Specification":"10ml"} 進行存儲
有什么插件或者思路
補充如下:
HTML結構:
且或且沈陽
屬于
不屬于
包含
不包含
為空
非空
開頭是
結尾是
重慶
屬于
不屬于
包含
不包含
為空
非空
開頭是
結尾是
屬于
不屬于
包含
不包含
為空
非空
開頭是
結尾是
西安
屬于
不屬于
包含
不包含
為空
非空
開頭是
結尾是
屬于
不屬于
包含
不包含
為空
非空
開頭是
結尾是
廣州
屬于
不屬于
包含
不包含
為空
非空
開頭是
結尾是
屬于
不屬于
包含
不包含
為空
非空
開頭是
結尾是
深圳
屬于
不屬于
包含
不包含
為空
非空
開頭是
結尾是
屬于
不屬于
包含
不包含
為空
非空
開頭是
結尾是
長春
屬于
不屬于
包含
不包含
為空
非空
開頭是
結尾是
屬于
不屬于
包含
不包含
為空
非空
開頭是
結尾是
上海
屬于
不屬于
包含
不包含
為空
非空
開頭是
結尾是
屬于
不屬于
包含
不包含
為空
非空
開頭是
結尾是
JS方法如下:(采用jQuery)
function getFilterTableData(tableID) {
var operatorMap = {
1: 'AND',
2: 'OR'
};
var conditionAndRangeMap = {
condition: function($obj) {
return $obj.children('select').val()
},
pickRange: function($obj) {
return {
val: $obj.children('select').val(),
mode: 'select'
}
},
editRange: function($obj) {
return {
val: $obj.children('input').val(),
mode: 'input'
}
},
nullRange: function($obj) {
return
}
};
var c = '.tableStyle-2-container',
h = '.tableStyle-2-operator',
b = '.tableStyle-2-content',
l = '.tableStyle-2-content-list',
li = '.tableStyle-2-content-list-unitRow',
label = '.unitRow-label';
var result = {};
var $start = $(tableID).children();
var looper = function($c, data) {
var $unitH = $c.children(h), //操作符
$unitB = $c.children(b), //內容
$unitB_children = $unitB.children(); //內容子節點
//如果有操作符
if($unitH.length) {
data.type = operatorMap[$unitH.attr('operator')];
}
//存儲子節點信息
data.children = [];
//如果內容有子節點
if($unitB_children.length) {
var $unitL = $unitB.children(l); //內容 - 列表
var $unitC = $unitB.children(c); //內容 - 容器
if($unitL.length) {
var children = [];
var $lis = $unitL.children(li);
for(var i = 0, length = $lis.length; i < length; i++) {
var $label = $lis.eq(i).children(label);
var $condition = $label.next();
var $range = $condition.next();
var unitRowData = $.extend({
col: $label.text()
}, {
rel: conditionAndRangeMap[$condition.attr('class').split('-')[1]]($condition),
}, conditionAndRangeMap[$range.attr('class').split('-')[1]]($range));
children.push(unitRowData);
}
data.children.push({
children: children
});
}
if($unitC.length) {
data.children.unshift({});
looper($unitC, data.children[0]);
}
}
}
looper($start, result);
return result
}
var resultData = getFilterTableData('#businessData-filter-table');
console.log(JSON.stringify(resultData));
總結
以上是生活随笔為你收集整理的把html转换为json数据,HTML转成JSON数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux基础(三):多重引导
- 下一篇: 《C#高级编程》读书笔记