简体   繁体   English

在 React 中将组件状态转换为 JSON

[英]Converting component state to JSON in React

I am new to React JS.我是 React JS 的新手。 I wanted to convert the component state data into a JSON object.我想将组件状态数据转换为 JSON 对象。 Is it possible?有可能吗? If so, how?如果是这样,如何?

Sample state :样品状态:

 this.state = { name : "Example", age : "21", description : "Some text here." }

That's the whole question.这就是整个问题。 I would appreciate it if you could tell me where I can find simple tutorials to do more advanced things in React (like sending/receiving JSON to/from a server, doing redirects, creating menus, etc).如果您能告诉我在哪里可以找到简单的教程来在 React 中做更高级的事情(例如向/从服务器发送/接收 JSON、进行重定向、创建菜单等),我将不胜感激。 I know I sound vague, and I don't expect very specific answers(if any) to this.我知道我听起来很含糊,我不希望对此有非常具体的答案(如果有的话)。

Just use JSON.stringify method in order to turn an javascript object into JSON text and store that JSON text in a string .只需使用JSON.stringify方法即可将javascript对象转换为JSON文本并将该JSON文本存储在字符串中

Also, you can use JSON.parse which turns a string of JSON text into a Javascript object.此外,您可以使用JSON.parse将一串JSON文本转换为Javascript对象。

 let state = { name : "Example", age : "21", description : "Some text here." } console.log(state); console.log(JSON.stringify(state));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM