2014-03-13 144 views
0

我已经看过类似的一些问题: How can I pretty-print JSON using JavaScript?Javascript: How to generate formatted easy-to-read JSON straight from an object?http://jsfiddle.net/AndyE/HZPVL/如何让JSON更清晰

但我不能让它为我工作。我一定做错了什么?任何人都可以建议吗?

我尝试添加 '\ t' 的字符串化的一部分,但它并没有给我

$(document).on("click", "#doBackupBtn", function(e) { 
    e.preventDefault(); 
    console.log("Begin backup process"); 

    $.when(
     backup("allergies") 

    ).then(function(allergies, log) { 
     console.log("All done"); 
     //Convert to JSON 
     var data = {allergies:allergies} 
     var serializedData = JSON.stringify(data, '\t'); 
     console.log(serializedData); 
     (function(console){ 

console.save = function(data, filename){ 

    if(!data) { 
     console.error('Console.save: No data') 
     return; 
    } 

    if(!filename) filename = 'console.json' 

    if(typeof data === "object"){ 
     data = JSON.stringify(data) 
    } 

    var blob = new Blob([data], {type: 'text/json'}), 
     e = document.createEvent('MouseEvents'), 
     a = document.createElement('a') 

    a.download = filename 
    a.href = window.URL.createObjectURL(blob) 
    a.dataset.downloadurl = ['text/json', a.download, a.href].join(':') 
    e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null) 
    a.dispatchEvent(e) 
} 
})(console) 

回答

1

第三个参数是\t。试试这个浏览器控制台(FF按Ctrl + Shift + K):

console.log(JSON.stringify({"a": {"b": "c"}}, null, 4));

结果:

"{ "a": { "b": "c" } }"

1

你缺少JSON.stringify工作(OBJ,不确定,2)