Javascript 打印 JSON 解析对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4933217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print JSON parsed object?
提问by Skizit
I've got a javascript object which has been JSON parsed using JSON.parse
I now want to print the object so I can debug it (something is going wrong with the function). When I do the following...
我有一个使用 JSON 解析的 javascript 对象,JSON.parse
我现在想打印该对象,以便我可以调试它(函数出现问题)。当我做以下...
for (property in obj) {
output += property + ': ' + obj[property]+'; ';
}
console.log(output);
I get multiple [object Object]'s listed. I'm wondering how would I print this in order to view the contents?
我列出了多个 [object Object]。我想知道如何打印它以查看内容?
采纳答案by RoToRa
Most debugger consoles support displaying objects directly. Just use
大多数调试器控制台支持直接显示对象。只需使用
console.log(obj);
Depending on your debugger this most likely will display the object in the console as a collapsed tree. You can open the tree and inspect the object.
根据您的调试器,这很可能会将对象在控制台中显示为折叠树。您可以打开树并检查对象。
回答by cHao
You know what JSON stands for? JavaScript Object Notation. It makes a pretty good format for objects.
你知道 JSON 代表什么吗? JavaScript 对象表示法。它为对象提供了一种非常好的格式。
JSON.stringify(obj)
will give you back a string representation of the object.
JSON.stringify(obj)
会给你一个对象的字符串表示。
回答by Distdev
回答by Lukasz Wiktor
If you want a pretty, multiline JSON with indentation then you can use JSON.stringify
with its 3rd argument:
如果你想要一个漂亮的多行 JSON 缩进,那么你可以使用JSON.stringify
它的第三个参数:
JSON.stringify(value[, replacer[, space]])
JSON.stringify(value[, replacer[, space]])
For example:
例如:
var obj = {a:1,b:2,c:{d:3, e:4}};
JSON.stringify(obj, null, " ");
or
或者
JSON.stringify(obj, null, 4);
will give you following result:
会给你以下结果:
"{
"a": 1,
"b": 2,
"c": {
"d": 3,
"e": 4
}
}"
In a browser console.log(obj)
does even better job, but in a shell console (node.js) it doesn't.
在浏览器console.log(obj)
中做得更好,但在 shell 控制台 (node.js) 中则不然。
回答by BERGUIGA Mohamed Amine
to Print JSON parsed object just type console.log( JSON.stringify(data, null, " ") );
and you will get output very clear
打印 JSON 解析的对象只需键入console.log( JSON.stringify(data, null, " ") );
,您将获得非常清晰的输出
回答by Dave Anderson
Use string formats;
使用字符串格式;
console.log("%s %O", "My Object", obj);
Chrome has Format Specifierswith the following;
Chrome 具有以下格式说明符;
%s
Formats the value as a string.%d
or%i
Formats the value as an integer.%f
Formats the value as a floating point value.%o
Formats the value as an expandable DOM element (as in the Elements panel).%O
Formats the value as an expandable JavaScript object.%c
Formats the output string according to CSS styles you provide.
%s
将值格式化为字符串。%d
或%i
将值格式化为整数。%f
将值格式化为浮点值。%o
将值格式化为可扩展的 DOM 元素(如在 Elements 面板中)。%O
将值格式化为可扩展的 JavaScript 对象。%c
根据您提供的 CSS 样式格式化输出字符串。
Firefox also has String Substitionswhich have similar options.
Firefox 也有具有类似选项的字符串替换。
%o
Outputs a hyperlink to a JavaScript object. Clicking the link opens an inspector.%d
or%i
Outputs an integer. Formatting is not yet supported.%s
Outputs a string.%f
Outputs a floating-point value. Formatting is not yet supported.
%o
输出指向 JavaScript 对象的超链接。单击该链接会打开一个检查器。%d
或%i
输出一个整数。尚不支持格式化。%s
输出一个字符串。%f
输出一个浮点值。尚不支持格式化。
Safari has printf style formatters
Safari 具有printf 样式的格式化程序
%d
or%i
Integer%[0.N]f
Floating-point value with N digits of precision%o
Object%s
String
%d
或%i
整数%[0.N]f
N 位精度的浮点值%o
目的%s
细绳
回答by diego matos - keke
Just use
只需使用
console.info("CONSOLE LOG : ")
console.log(response);
console.info("CONSOLE DIR : ")
console.dir(response);
and you will get this in chrome console :
你会在 chrome 控制台中得到这个:
CONSOLE LOG :
facebookSDK_JS.html:56 Object {name: "Diego Matos", id: "10155988777540434"}
facebookSDK_JS.html:57 CONSOLE DIR :
facebookSDK_JS.html:58 Objectid: "10155988777540434"name: "Diego Matos"__proto__: Object
回答by mbenhalima
Nice and simple:
漂亮而简单:
console.log("object: %O", obj)
回答by Ruwantha
If you want to debug why not use console debug
如果要调试为什么不使用控制台调试
window.console.debug(jsonObject);
回答by Rayiez
Simple function to alert contents of an object or an array .
Call this function with an array or string or an object it alerts the contents.
提醒对象或数组内容的简单函数。
使用数组或字符串或它提醒内容的对象调用此函数。
Function
功能
function print_r(printthis, returnoutput) {
var output = '';
if($.isArray(printthis) || typeof(printthis) == 'object') {
for(var i in printthis) {
output += i + ' : ' + print_r(printthis[i], true) + '\n';
}
}else {
output += printthis;
}
if(returnoutput && returnoutput == true) {
return output;
}else {
alert(output);
}
}
Usage
用法
var data = [1, 2, 3, 4];
print_r(data);