2016-10-03 58 views
0

我已经设法从一个API输出JSON,但是它在一个大块中输出,但是我希望在每一次关闭时在一个新行上显示每一片json'} ”。在新行上反应输出JSON

import React, { Component } from 'react'; 
import './App.css'; 
import $ from 'jquery'; 

class App extends Component { 

    state = {result : null} 

    componentDidMount =() => { 

    $.ajax({ 
     type: "GET", 
     url: 'http://localhost:3001/data', 
     data: {}, 
     xhrFields: { 
     withCredentials: false 
     }, 
     crossDomain: true, 
     dataType: 'JSON', 
     success: (result) => { 
     this.setState({result : result}); 
     } 
    }); 

    }; 

    render() { 
    return (
      <div className="App"> 
      <header> 
       <h2>Last Application Deployment </h2> 
      </header> 
      <div id='renderhere'> 
       {JSON.stringify(this.state.result)} 
      </div> 
      </div> 
     ); 
    } 
    } 

export default App; 

回答

0

第一种选择:如果要输出JSON用于调试的目的,你应该尝试react-json-pretty

第二个选项:如果你试图做的是应该是生产什么做这样的事情:

<div id='renderhere'> 
    <pre>{JSON.stringify(this.state.result, null, 2)}</pre> 
</div> 
+0

谢谢你的回答,但是,我已经在使用'JSON.stringify()',我试图实现的是,而不是输出JSON作为一个整体块,我希望JSON中的每个对象被输出到浏览器上,如果不清楚,很抱歉。 – DaveDavidson

+0

对不起,我的代码示例已损坏,请再次检查答案。 – Shota

+0

**太棒了!**谢谢。 – DaveDavidson