如何在文本区域 React js 中呈现漂亮的 json 数据?

vig*_*ran 2 javascript jquery json reactjs

我是响应 js 的新手。我在 textarea 中渲染漂亮的 json 数据时遇到问题。我不知道哪个部分错了我希望我的漂亮的json在 textarea 中渲染像这样

"email":"xxxx@x.com",
"email":"yyyy@y.com",
.....
Run Code Online (Sandbox Code Playgroud)

这是我的代码但我的 textarea 中什么也没有

/**
 * Created by arfo on 6/26/2016.
 */
var React =require('react');
var api = require('../utils');


var Bulkmail = React.createClass({
    getInitialState:function () {
      return{
          default:10,
          data:[],
          color:'#58FA58'

      }
    },
    componentDidMount:function () {
        api.getemail(this.state.default).then(function (response) {
           this.setState({
               data:response

           })
        }.bind(this))
    },
    onSubmit:function (e) {
      e.preventDefault();
      console.log(this.refs.text.value.trim());


    },

    onChange:function (e) {
     e.preventDefault();
        //console.log(this.refs.text.value.trim())
        var data = this.refs.text.value.trim();
        if(isNaN(data)){
            this.setState({
                color:'#FE2E2E'
            })
        }else{
            this.setState({
                color:'#58FA58'
            })
        }
    },
    render:function () {
        console.log(this.state.data);
        var results = this.state.data;
        return(
           <div className="bodybox">
               <div className="box">
                  <div className="upc">
                      <p>Generate Bulk Email</p>
                      <form onSubmit={this.onSubmit}>
                      <input onChange={this.onChange} type="text" style={{border:'1px solid '+this.state.color}}  ref="text" defaultValue={this.state.default} placeholder="Enter Number"/>
                       <button>Get Data</button>
                      </form>
                      <div className="result">
                          <ul>
                              {this.state.data.map(function (data) {
                                  return  <li key={data.id}>{data.email}</li>
                              })}

                          </ul>
                      </div>

                  </div>
                   <div className="tdown">

                       <p>Json Format</p>

                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                       <textarea defaultValue={this.state.data.map(function(data) {
                           return JSON.stringify(data.email)
                       })}  >
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                       </textarea>
                   </div>
               </div>
           </div>
        )
    }
});

module.exports = Bulkmail ;
Run Code Online (Sandbox Code Playgroud)

Ino*_*ani 7

无需使用困难的正则表达式,我们可以使用 JSON.stringify(object, undefined, 2) 中的功能从 JSON 中获取精美呈现的字符串。

var obj={"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}


var pretty = JSON.stringify(obj, undefined, 2);

var ugly = document.getElementById('myTextArea').value; document.getElementById('myTextArea').value = pretty;
Run Code Online (Sandbox Code Playgroud)


Kok*_*lav 2

更新

当然你应该使用 JSON.stringify:

JSON.stringify(your_object, undefined, desired_indentation)
Run Code Online (Sandbox Code Playgroud)

旧答案:

 <textarea value={this.state.data.map(e=>JSON.stringify(e))}  defaultValue="val" />
Run Code Online (Sandbox Code Playgroud)

结果 {"email":"some@mail"},{"email":"some@mail"},{"email":"some@mail"}


let value = this.state.data.map(e=>JSON.stringify(e).replace(/{|}/g,''));

<textarea value={value}  defaultValue="val" />
Run Code Online (Sandbox Code Playgroud)

结果 "email" : "xx@yy.y", "email" : "some@mail", "email" : "some@mail"


 let value = this.state.data.map(e=>JSON.stringify(e).replace(/{|}/g,'')).join(',\n');

 <textarea value={value}  defaultValue="val" />
Run Code Online (Sandbox Code Playgroud)

结果 "email" : "xx@yy.y", "email" : "some@mail", "email" : "some@mail"

在 HTML 中, 的值是通过子项设置的。在 React 中,你应该使用 value 来代替。