jolie empreinte JSON en utilisant JavaScript

Mots clés : javascriptjsonpretty-printjavascript

meilleur 5 Réponses jolie empreinte JSON en utilisant JavaScript

vote vote

99

var str = JSON.stringify(obj, null, 2); // spacing level = 2 
function syntaxHighlight(json) {     if (typeof json != 'string') {          json = JSON.stringify(json, undefined, 2);     }     json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');     return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {         var cls = 'number';         if (/^"/.test(match)) {             if (/:$/.test(match)) {                 cls = 'key';             } else {                 cls = 'string';             }         } else if (/true|false/.test(match)) {             cls = 'boolean';         } else if (/null/.test(match)) {             cls = 'null';         }         return '<span class="' + cls + '">' + match + '</span>';     }); } 
function output(inp) {      document.body.appendChild(document.createElement('pre')).innerHTML = inp;  }    function syntaxHighlight(json) {      json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');      return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {          var cls = 'number';          if (/^"/.test(match)) {              if (/:$/.test(match)) {                  cls = 'key';              } else {                  cls = 'string';              }          } else if (/true|false/.test(match)) {              cls = 'boolean';          } else if (/null/.test(match)) {              cls = 'null';          }          return '<span class="' + cls + '">' + match + '</span>';      });  }    var obj = {a:1, 'b':'foo', c:[false,'false',null, 'null', {d:{e:1.3e5,f:'1.3e5'}}]};  var str = JSON.stringify(obj, undefined, 4);    output(str);  output(syntaxHighlight(str));
pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }  .string { color: green; }  .number { color: darkorange; }  .boolean { color: blue; }  .null { color: magenta; }  .key { color: red; }
vote vote

86

var jsonString = '{"some":"json"}'; var jsonPretty = JSON.stringify(JSON.parse(jsonString),null,2);   
vote vote

79

JSON.stringify(jsonobj,null,'\t') 
vote vote

67

var jsonObj = {"streetLabel": "Avenue Anatole France", "city": "Paris 07",  "postalCode": "75007", "countryCode": "FRA",  "countryLabel": "France" };  document.getElementById("result-before").innerHTML = JSON.stringify(jsonObj); 
document.getElementById("result-after").innerHTML = "<pre>"+JSON.stringify(jsonObj,undefined, 2) +"</pre>" 
var jsonObj = {"streetLabel": "Avenue Anatole France", "city": "Paris 07",  "postalCode": "75007", "countryCode": "FRA",  "countryLabel": "France" };    document.getElementById("result-before").innerHTML = JSON.stringify(jsonObj);    document.getElementById("result-after").innerHTML = "<pre>"+JSON.stringify(jsonObj,undefined, 2) +"</pre>"
div { float:left; clear:both; margin: 1em 0; }
<div id="result-before"></div>  <div id="result-after"></div>
vote vote

55

function JSONstringify(json) {     if (typeof json != 'string') {         json = JSON.stringify(json, undefined, '\t');     }      var          arr = [],         _string = 'color:green',         _number = 'color:darkorange',         _boolean = 'color:blue',         _null = 'color:magenta',         _key = 'color:red';      json = json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {         var style = _number;         if (/^"/.test(match)) {             if (/:$/.test(match)) {                 style = _key;             } else {                 style = _string;             }         } else if (/true|false/.test(match)) {             style = _boolean;         } else if (/null/.test(match)) {             style = _null;         }         arr.push(style);         arr.push('');         return '%c' + match + '%c';     });      arr.unshift(json);      console.log.apply(console, arr); } 
javascript:function JSONstringify(json) {if (typeof json != 'string') {json = JSON.stringify(json, undefined, '\t');}var arr = [],_string = 'color:green',_number = 'color:darkorange',_boolean = 'color:blue',_null = 'color:magenta',_key = 'color:red';json = json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {var style = _number;if (/^"/.test(match)) {if (/:$/.test(match)) {style = _key;} else {style = _string;}} else if (/true|false/.test(match)) {style = _boolean;} else if (/null/.test(match)) {style = _null;}arr.push(style);arr.push('');return '%c' + match + '%c';});arr.unshift(json);console.log.apply(console, arr);};void(0); 
var obj = {a:1, 'b':'foo', c:[false,null, {d:{e:1.3e5}}]}; JSONstringify(obj); 
json = json.replace(/%/g, '%%'); 

Questions similaires