简体   繁体   English

Javascript:将Console.debug()输出写入浏览器?

[英]Javascript: Write Console.debug() output to browser?

I need to be able to take any JSON data and print the key/value pairs. 我需要能够获取任何JSON数据并打印键/值对。

(something similar to print_r() in PHP) (类似于PHP中的print_r())

Is this even possible with javascript? 这甚至可以使用JavaScript吗?

I usually just quickly create a log function that allows you change the logging method. 我通常只是快速创建一个日志功能,允许您更改日志记录方法。 Write enablers/disablers or comment out to choose the options. 写入启用/禁用或注释以选择选项。

function log(msg){
  if (window.console && console.log) {
    console.log(msg); //for firebug
  }
  document.write(msg); //write to screen
  $("#logBox").append(msg); //log to container
}

Update: Info on Firebug's Console API 更新:有关Firebug 控制台API的信息

Update: Added check for non firebug browsers. 更新:添加了对非firebug浏览器的检查。

Yes, you can process a surprising amount of info through alert, and you can also use it for debugging. 是的,您可以通过警报处理大量信息,您也可以使用它进行调试。

Here is a print_r equivalent for javascript also. 这里是javascript的print_r 等价物

function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}

good luck with your project! 祝你的项目好运!

I would recommend you to get a JSON parsing library like JSON2 for being able to "stringify" your objects, then you can simply: 我建议你获得一个像JSON2这样的JSON解析库,以便能够“对字符串化”你的对象,然后你可以简单地:

var myString = JSON.stringify(myObject);

myString will now contain a string representation of myObject . myString现在将包含myObject的字符串表示形式。

But if it's for debugging purposes I would recommend you to get a JavaScript debugger, like Firebug , you get a lot of useful functions in the Console API . 但是如果它是出于调试目的,我会建议你使用一个JavaScript调试器,比如Firebug ,你在Console API中获得了很多有用的功能。

你能简单地使用以下内容:

 document.write('<h2>Your Text and or HTML here.</h2>'); 

FireBug is a great tool! FireBug是一个很棒的工具! Indispensable! 必不可少! I found it eliminates the need to write debug data into my pages and I can view JSON all day long. 我发现它不需要将调试数据写入我的页面,我可以整天查看JSON。

I need to be able to take any JSON data and print the key/value pairs. 我需要能够获取任何JSON数据并打印键/值对。

Well then print the JSON data. 然后打印JSON数据。 JSON is a notation, not an object. JSON是一种符号,而不是一个对象。 If you have JSON data, you already have all you need. 如果您有JSON数据,那么您已经拥有了所需的一切。 If you want it to be a little more fancy, you might want to add a linebreak after every "\\s*, . 如果你想要它更加花哨,你可能想在每个"\\s*,之后添加一个换行符。

If you want to deconstruct an object, it's not possible unless you're using JavaScript, as ECMAScript can't create cyclic references in a single object literal. 如果你想解构一个对象,除非你使用JavaScript,否则它是不可能的,因为ECMAScript不能在单个对象文字中创建循环引用。 If this is JavaScript-only, then you can use uneval(object) , which will make use of sharp variables. 如果这只是JavaScript,那么你可以使用uneval(object) ,它将使用尖锐的变量。 (eg. ({x:#1={y:#1#}}) ). (例如。 ({x:#1={y:#1#}}) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM