Javascript JSON 到字符串变量转储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3334341/
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
JSON to string variable dump
提问by ina
Is there a quick function to convert JSON objects received via jQuery getJSONto a string variable dump (for tracing/debugging purposes)?
是否有快速函数将通过接收的 JSON 对象转换jQuery getJSON为字符串变量转储(用于跟踪/调试目的)?
回答by Anders
Yes, JSON.stringify, can be found here, it's includedin Firefox 3.5.4 and above.
是的,JSON.stringify可以在这里找到,它包含在 Firefox 3.5.4 及更高版本中。
A JSON stringifier goes in the opposite direction, converting JavaScript data structures into JSON text. JSON does not support cyclic data structures, so be careful to not give cyclical structures to the JSON stringifier.https://web.archive.org/web/20100611210643/http://www.json.org/js.html
JSON stringifier 则相反,将 JavaScript 数据结构转换为 JSON 文本。JSON 不支持循环数据结构,因此请注意不要为 JSON 字符串化符提供循环结构。https://web.archive.org/web/20100611210643/http://www.json.org/js.html
var myJSONText = JSON.stringify(myObject, replacer);
回答by Nick Craver
You can use console.log()in Firebug or Chrome to get a good object view here, like this:
您可以console.log()在 Firebug 或 Chrome 中使用以获得良好的对象视图,如下所示:
$.getJSON('my.json', function(data) {
console.log(data);
});
If you just want to viewthe string, look at the Resource view in Chromeor the Net view in Firebugto see the actual string response from the server (no need to convert it...you received it this way).
如果您只想查看字符串,请查看 Chrome中的Resource 视图或Firebug 中的Net 视图,以查看来自服务器的实际字符串响应(无需转换……您以这种方式接收)。
If you want to take that string and break it down for easy viewing, there's an excellent tool here: http://json.parser.online.fr/
如果您想将该字符串分解以便于查看,这里有一个很好的工具:http: //json.parser.online.fr/
回答by Ties
i personally use the jquery dump pluginalot to dump objects, its a bit similar to php's print_r() function Basic usage:
我个人使用jquery dump 插件很多来转储对象,它有点类似于 php 的 print_r() 函数基本用法:
var obj = {
hubba: "Some string...",
bubba: 12.5,
dubba: ["One", "Two", "Three"]
}
$("#dump").append($.dump(obj));
/* will return:
Object {
hubba: "Some string..."
bubba: 12.5
dubba: Array (
0 => "One"
1 => "Two"
2 => "Three"
)
}
*/
Its very human readable, i also recommend this site http://json.parser.online.fr/for creating/parsing/reading json, because it has nice colors
它非常易读,我也推荐这个站点http://json.parser.online.fr/用于创建/解析/读取 json,因为它有漂亮的颜色
回答by user579338
Here is the code I use. You should be able to adapt it to your needs.
这是我使用的代码。您应该能够使其适应您的需求。
function process_test_json() {
var jsonDataArr = { "Errors":[],"Success":true,"Data":{"step0":{"collectionNameStr":"dei_ideas_org_Private","url_root":"http:\/\/192.168.1.128:8500\/dei-ideas_org\/","collectionPathStr":"C:\ColdFusion8\wwwroot\dei-ideas_org\wwwrootchapter0-2\verity_collections\","writeVerityLastFileNameStr":"C:\ColdFusion8\wwwroot\dei-ideas_org\wwwroot\chapter0-2\VerityLastFileName.txt","doneFlag":false,"state_dbrec":{},"errorMsgStr":"","fileroot":"C:\ColdFusion8\wwwroot\dei-ideas_org\wwwroot"}}};
var htmlStr= "<h3 class='recurse_title'>[jsonDataArr] struct is</h3> " + recurse( jsonDataArr );
alert( htmlStr );
$( document.createElement('div') ).attr( "class", "main_div").html( htmlStr ).appendTo('div#out');
$("div#outAsHtml").text( $("div#out").html() );
}
function recurse( data ) {
var htmlRetStr = "<ul class='recurseObj' >";
for (var key in data) {
if (typeof(data[key])== 'object' && data[key] != null) {
htmlRetStr += "<li class='keyObj' ><strong>" + key + ":</strong><ul class='recurseSubObj' >";
htmlRetStr += recurse( data[key] );
htmlRetStr += '</ul ></li >';
} else {
htmlRetStr += ("<li class='keyStr' ><strong>" + key + ': </strong>"' + data[key] + '"</li >' );
}
};
htmlRetStr += '</ul >';
return( htmlRetStr );
}
</script>
</head><body>
<button onclick="process_test_json()" >Run process_test_json()</button>
<div id="out"></div>
<div id="outAsHtml"></div>
</body>
回答by hotzen
something along this?
沿着这个?
function dump(x, indent) {
var indent = indent || '';
var s = '';
if (Array.isArray(x)) {
s += '[';
for (var i=0; i<x.length; i++) {
s += dump(x[i], indent)
if (i < x.length-1) s += ', ';
}
s +=']';
} else if (x === null) {
s = 'NULL';
} else switch(typeof x) {
case 'undefined':
s += 'UNDEFINED';
break;
case 'object':
s += "{ ";
var first = true;
for (var p in x) {
if (!first) s += indent + ' ';
s += p + ': ';
s += dump(x[p], indent + ' ');
s += "\n"
first = false;
}
s += '}';
break;
case 'boolean':
s += (x) ? 'TRUE' : 'FALSE';
break;
case 'number':
s += x;
break;
case 'string':
s += '"' + x + '"';
break;
case 'function':
s += '<FUNCTION>';
break;
default:
s += x;
break;
}
return s;
}

