Pretty Printing JSON with React

后端 未结 5 2268
醉酒成梦
醉酒成梦 2020-12-23 14:23

I\'m using ReactJS and part of my app requires pretty printed JSON.

I get some JSON like: { \"foo\": 1, \"bar\": 2 }, and if I run that through

5条回答
  •  无人及你
    2020-12-23 14:45

    const getJsonIndented = (obj) => JSON.stringify(newObj, null, 4).replace(/["{[,\}\]]/g, "")
    
    const JSONDisplayer = ({children}) => (
        
    {getJsonIndented(children)}
    )

    Then you can easily use it:

    const Demo = (props) => {
       ....
       return {someObj}
    }
    

提交回复
热议问题