Created
October 21, 2022 03:13
-
-
Save leoborlot/eff3ff02dcd1a773eb14e02e77d5d1d8 to your computer and use it in GitHub Desktop.
Json Pretty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <pre> | |
| <code class="json"> | |
| {"id":"3436253533052629","first_name":"AAA","last_name":"BBB","name":"測試","email":"dd@gmail.com"} | |
| </code> | |
| </pre> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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, '&').replace(/\\"/g, '"') | |
| .replace(/</g, '<').replace(/>/g, '>') | |
| .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)); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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