JavaScriptのオブジェクトをJSONの文字列(Pretty print)にする
JavaScriptの文字列をJSON文字列にするだけならJSON.stringifyを使えばいいんだけど、見やすい形にしたいので。
デバッグ用にちょっとだけ欲しかったりするんだけど、前に書いたコードを無くしてしまったのでここにメモっておこうかと思う。他にもっといい方法があったら誰かおしえてください。。
/** * @param obj Javascriptのオブジェクト * @param baseIndent インデントの数 * @return 整形されたJSON文字列 */ function prettyPrint(obj, baseIndent) { var buff = []; var addIndent = function(buff, indent) { for (i=0; i<indent; i++) { buff.push(' '); } } var read = function(o, indent) { if (typeof(o) == "string" || o instanceof String) { return '"' + o + '"'; } else if (typeof(o) == "number" || o instanceof Number) { return o; } else if (o instanceof Array) { if (o) { buff.push('['); for (idx in o) { buff.push(read(o[idx], indent + baseIndent)); buff.push(', '); } if (o.length > 0) delete buff[buff.length - 1]; buff.push(']'); } } else if (o instanceof Object) { if (o) { buff.push('{\n'); for (key in o) { addIndent(buff, indent + baseIndent); buff.push('"' + key + '": '); buff.push(read(o[key], indent + baseIndent)); buff.push(',\n'); } delete buff[buff.length - 1]; buff.push('\n'); addIndent(buff, indent); buff.push('}'); } } } read(obj, 0); return buff.join(''); }
こんなデータをいれると、
prettyPrint({name: "hironemu", address: "Tokyo", array: [1, 2, 3, 4, {test: "abc", sample: 500}, 6, 7, 8]}, 2);
こうなった。
{ "name": "hironemu", "address": "Tokyo", "array": [1, 2, 3, 4, { "test": "abc", "sample": 500 }, 6, 7, 8] }