php html转换json数据,javascript - How to convert HTML to JSON using PHP? - Stack Overflow

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);

}

dcb4c780679ce694903993c23bf6cb6a.png

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:

b94f7518620de58f490ceff08dfa5a55.png .. How to achieve the same solution?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值