Skip to content

Instantly share code, notes, and snippets.

@leoborlot
Created October 21, 2022 03:13
Show Gist options
  • Select an option

  • Save leoborlot/eff3ff02dcd1a773eb14e02e77d5d1d8 to your computer and use it in GitHub Desktop.

Select an option

Save leoborlot/eff3ff02dcd1a773eb14e02e77d5d1d8 to your computer and use it in GitHub Desktop.
Json Pretty
<pre>
<code class="json">
{"id":"3436253533052629","first_name":"AAA","last_name":"BBB","name":"測試","email":"dd@gmail.com"}
</code>
</pre>
(function(win){
var zp = win.zp || {};
win.zp = zp;
zp.json = {
replacer: function(match, pIndent, pKey, pVal, pEnd) {
var key = '<span style="color: blue;">';
var val = '<span style="color: navy;">';
var str = '<span style="color: green;">';
var r = pIndent || '';
if (pKey)
r = r + key + pKey.replace(/[": ]/g, '') + '</span>: ';
if (pVal)
r = r + (pVal[0] == '"' ? str : val) + pVal + '</span>';
return r + (pEnd || '');
},
pretty: function(obj) {
var jsonLine = /^( *)("[\w]+": )?("[^"]*"|[\w.+-]*)?([,[{])?$/mg;
return JSON.stringify(obj, null, 3)
.replace(/&/g, '&amp;').replace(/\\"/g, '&quot;')
.replace(/</g, '&lt;').replace(/>/g, '&gt;')
.replace(jsonLine, zp.json.replacer);
}
};
})(window);
$(".json").each(function( index ) {
var jsonStr = $(this).html();
var jsonOBJ = JSON.parse(jsonStr);
$(this).html(zp.json.pretty(jsonOBJ));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment