$(document).ready(function(){ $('#btn').click(function(){ var jsonStr = $('#jsonData').val(); var json = eval('('+jsonStr+')'); (function(){ var print_r = function(o, depth) { var result = ''; depth || (depth=1); var indent = new Array(4*depth+1).join(' '); var indentNext = new Array(4*(depth+1)+1).join(' '); var indentNextTwo = new Array(4*(depth+2)+1).join(' '); var tmp = ''; var type = typeof o; switch(type) { case 'string': case 'number': case 'boolean': case 'undefined': case 'function': tmp += indent + indentNext + o + "n"; break; case 'object': default: for(var key in o) { tmp += indentNextTwo + '[' + key + '] = '; tmp += print_r(o[key], (depth+1)); } } result += type + "n"; result += indentNext + '(' + "n"; result += tmp; result += indentNext + ')' + "n"; return result; }; alert(print_r(json)); }(json)); }); });
if (return_val !== true) { if (d.body) { this.echo(output); } else { try { d = XULDocument; // We're in XUL, so appending as plain text won't work; trigger an error out of XUL this.echo('<pre xmlns="http://www.w3.org/1999/xhtml" style="white-space:pre;">' + output + '</pre>'); } catch (e) { this.echo(output); // Outputting as plain text may work in some plain XML } } return true; } return output; }
发布评论
评论(4)
分享我在用的一个方案:
function print_r(theObj) {
var retStr = '';
if (typeof theObj == 'object') {
retStr += '<div style="font-family:Tahoma; font-size:7pt;">';
for (var p in theObj) {
if (typeof theObj[p] == 'object') {
retStr += '<div><b>['+p+'] => ' + typeof(theObj) + '</b></div>';
retStr += '<div style="padding-left:25px;">' + print_r(theObj[p]) + '</div>';
} else {
retStr += '<div>['+p+'] => <b>' + theObj[p] + '</b></div>';
}
}
retStr += '</div>';
}
return retStr;
}
openjs的print_r实现:
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;
//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += " ";
if(typeof(arr) == 'object') { //Array/Hashes/Objects
for(var item in arr) {
var value = arr[item];
if(typeof(value) == 'object') { //If it is an array,
dumped_text += level_padding + "'" + item + "' ...n";
dumped_text += dump(value,level+1);
} else {
dumped_text += level_padding + "'" + item + "' => "" + value + ""n";
}
}
} else { //Stings/Chars/Numbers etc.
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}
你这个问题百度上有很多的,js没有现成的函数,需要自己封装,给你一个:
$(document).ready(function(){
$('#btn').click(function(){
var jsonStr = $('#jsonData').val();
var json = eval('('+jsonStr+')');
(function(){
var print_r = function(o, depth) {
var result = '';
depth || (depth=1);
var indent = new Array(4*depth+1).join(' ');
var indentNext = new Array(4*(depth+1)+1).join(' ');
var indentNextTwo = new Array(4*(depth+2)+1).join(' ');
var tmp = '';
var type = typeof o;
switch(type) {
case 'string':
case 'number':
case 'boolean':
case 'undefined':
case 'function':
tmp += indent + indentNext + o + "n";
break;
case 'object':
default:
for(var key in o) {
tmp += indentNextTwo + '[' + key + '] = ';
tmp += print_r(o[key], (depth+1));
}
}
result += type + "n";
result += indentNext + '(' + "n";
result += tmp;
result += indentNext + ')' + "n";
return result;
};
alert(print_r(json));
}(json));
});
});
给你推荐个网站 phpjs,这里有php函数的js实现
phpjs
function print_r (array, return_val) {
// Prints out or returns information about the specified variable
//
// version: 1109.2015
// discuss at: http://phpjs.org/functions/print_r
// + original by: Michael White (http://getsprink.com)
// + improved by: Ben Bryan
// + input by: Brett Zamir (http://brett-zamir.me)
// + improved by: Brett Zamir (http://brett-zamir.me)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// - depends on: echo
// * example 1: print_r(1, true);
// * returns 1: 1
var output = '',
pad_char = ' ',
pad_val = 4,
d = this.window.document,
getFuncName = function (fn) {
var name = (/W*functions+([w$]+)s*(/).exec(fn);
if (!name) {
return '(Anonymous)';
}
return name[1];
},
repeat_char = function (len, pad_char) {
var str = '';
for (var i = 0; i < len; i++) {
str += pad_char;
}
return str;
},
formatArray = function (obj, cur_depth, pad_val, pad_char) {
if (cur_depth > 0) {
cur_depth++;
}
var base_pad = repeat_char(pad_val * cur_depth, pad_char);
var thick_pad = repeat_char(pad_val * (cur_depth + 1), pad_char);
var str = '';
if (typeof obj === 'object' && obj !== null && obj.constructor && getFuncName(obj.constructor) !== 'PHPJS_Resource') {
str += 'Arrayn' + base_pad + '(n';
for (var key in obj) {
if (Object.prototype.toString.call(obj[key]) === '[object Array]') {
str += thick_pad + '[' + key + '] => ' + formatArray(obj[key], cur_depth + 1, pad_val, pad_char);
}
else {
str += thick_pad + '[' + key + '] => ' + obj[key] + 'n';
}
}
str += base_pad + ')n';
}
else if (obj === null || obj === undefined) {
str = '';
}
else { // for our "resource" class
str = obj.toString();
}
return str;
};
output = formatArray(array, 0, pad_val, pad_char);
if (return_val !== true) {
if (d.body) {
this.echo(output);
}
else {
try {
d = XULDocument; // We're in XUL, so appending as plain text won't work; trigger an error out of XUL
this.echo('<pre xmlns="http://www.w3.org/1999/xhtml" style="white-space:pre;">' + output + '</pre>');
} catch (e) {
this.echo(output); // Outputting as plain text may work in some plain XML
}
}
return true;
}
return output;
}