2016-10-03 59 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