pretty-print JSON using JavaScript

后端 未结 24 1674
一向
一向 2020-11-21 06:45

How can I display JSON in an easy-to-read (for human readers) format? I\'m looking primarily for indentation and whitespace, with perhaps even colors / font-styles / etc.

24条回答
  •  滥情空心
    2020-11-21 07:04

    If you're looking for a nice library to prettify json on a web page...

    Prism.js is pretty good.

    http://prismjs.com/

    I found using JSON.stringify(obj, undefined, 2) to get the indentation, and then using prism to add a theme was a good approach.

    If you're loading in JSON via an ajax call, then you can run one of Prism's utility methods to prettify

    For example:

    Prism.highlightAll()
    

提交回复
热议问题