I can convert JSON to HTML using JsontoHtml library. Now,I need to convert present HTML to JSON as shown in this site. When looked into the code I found the following script:
$(function(){
//HTML to JSON
$('#btn-render-json').click(function() {
//Set html output
$('#html-output').html( $('#html-input').val() );
//Process to JSON and format it for consumption
$('#html-json').html( FormatJSON(toTransform($('#html-output').children())) );
//Convert obj or array to transform
function toTransform(obj) {
var json;
if( obj.length > 1 )
json = [];
for(var i = 0; i < obj.length; i++)
json[json.length++] = ObjToTransform(obj[i]);
} else
json = ObjToTransform(obj);
return(json);
//Convert obj to transform
function ObjToTransform(obj)
//Get the DOM element
var el = $(obj).get(0);
//Add the tag element
var json = {'tag':el.nodeName.toLowerCase()};
for (var attr, i=0, attrs=el.attributes, l=attrs.length; i
attr = attrs[i];
json[attr.nodeName] = attr.value;
var children = $(obj).children();
if( children.length > 0 ) json['children'] = [];
else json['html'] = $(obj).text();
//Add the children
for(var c = 0; c < children.length; c++)
json['children'][json['children'].length++] = toTransform(children[c]);
return(json);
//Format JSON (with indents)
function FormatJSON(oData, sIndent) {
if (arguments.length < 2) {
var sIndent = "";
var sIndentStyle = " ";
var sDataType = RealTypeOf(oData);
// open object
if (sDataType == "array") {
if (oData.length == 0) {
return "[]";
var sHTML = "[";
} else {
var iCount = 0;
$.each(oData, function() {
iCount++;
return;
if (iCount == 0) { // object is empty
return "{}";
var sHTML = "{";
// loop through items
var iCount = 0;
$.each(oData, function(sKey, vValue) {
if (iCount > 0) {
sHTML += ",";
if (sDataType == "array") {
sHTML += ("\n" + sIndent + sIndentStyle);
} else {
sHTML += ("\"" + sKey + "\"" + ":");
// display relevant data type
switch (RealTypeOf(vValue)) {
case "array":
case "object":
sHTML += FormatJSON(vValue, (sIndent + sIndentStyle));
break;
case "boolean":
case "number":
sHTML += vValue.toString();
break;
case "null":
sHTML += "null";
break;
case "string":
sHTML += ("\"" + vValue + "\"");
break;
default:
sHTML += ("TYPEOF: " + typeof(vValue));
// loop
iCount++;
// close object
if (sDataType == "array") {
sHTML += ("\n" + sIndent + "]");
} else {
sHTML += ("}");
// return
return sHTML;
//Get the type of the obj (can replace by jquery type)
function RealTypeOf(v) {
if (typeof(v) == "object") {
if (v === null) return "null";
if (v.constructor == (new Array).constructor) return "array";
if (v.constructor == (new Date).constructor) return "date";
if (v.constructor == (new RegExp).constructor) return "regex";
return "object";
return typeof(v);
Now, I am in need of using the following function in PHP. I can get the HTML data. All what I needed now is to convert the JavaScript function to PHP function. Is this possible? My major doubts are as follows:
The primary input for the Javascript function toTransform() is an object. Is it possible to convert HTML to object via PHP?
Are all the functions present in this particular JavaScript available in PHP?
Please suggest me the idea.
When I tried to convert script tag to json as per the answer given, I get errors. When I tried it in json2html site, it showed like this:
.. How to achieve the same solution?
背景商城的详情页面使用百度的富文本,存储在mysql中。前端使用vue,所以在去取出来和其他
数据
一起
转
为
json
数据
返回给前端的时候发生错误,
json
格式错误。打印发现是富文本里面有图片或者说是有 “” 双引号的原因,在网上找了一些方法说需要
转
义再存。后来觉得这种方法不是很适合,所以换了一种方法,使用 Base64 的方式
转
为
json
转
为
json
前先把富文本的内容base64
转
化一下,这样jso...
|我搜索了几样东西之后尝试了。它不起作用。请帮助我---------- jquery函数----------------------------function show_reqfunc_to_box(this_id){var enc_item_id = this_id;enc_item_id = enc_item_id.split(\"--\")[1];reqfunc_to_item_id_hi...
json
字符串怎
转换
成
json
对象?js 字符串怎么
转
json
对象
JSON
.parse(
json
str); //可以将js字符串
转换
成
json
对象js怎么把字符串
转换
成
json
例如:
JSON
字符串: var str1 = '{ "name": "cxh", "sex": "man" }';
JSON
对象: var str2 = { "name": "cxh", "sex": "man" }; 一...
一、
json
_encode()该函数主要用来将数组和对象,
转换
为
json
格式。先看一个数组
转换
的例子:$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);echo
json
_encode($arr);结果为{"a":1,"b":2,"c":3,"d":4,"e":5}再看一个对象
转换
的例子:$obj->body ...
可能你的
数据
是这样的:
html
_data = """Card balance$18.30Card nameNAMEnAccount holderNAMECard number1234StatusActive"""我们可以使用以下代码将结果作为列表获取:from bs4 import BeautifulSouptable_data = [[cell.text for cell in row("td"...
这次给大家带来
html
里table表
数据
如何
转
为
Json
格式,
html
里table表
数据
转
为
Json
格式的注意事项有哪些,下面就是实战案例,一起来看一下。表
数据
转
Json
格式的
javascript
函数如下var keysArr = new Array("key0", "key1","key2");function TableTo
Json
(tableid) { //tableid是你要
转
化的表的...