Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How nodejs converts a string to an object

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how nodejs converts a string into an object, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

Nodejs converts a string into an object: 1, use the eval () function, syntax "eval ('('+ str+')')"; 2, use "JSON.parse ()" function, syntax "JSON.parse (str)".

This tutorial operating environment: windows7 system, nodejs version 12.19.0, DELL G3 computer.

Conversion between NodeJS object and string

String transfer object:

1, use the eval function

Var str=' {"F001": true, "F002": false, "F003": false}'; var obj = eval ('('+ str+')'); / / you must put parentheses to convert the string to an object, and parentheses indicate running the code inside. Console.log (obj.F001); / / true

2, use the JSON.parse function

Var str=' {"F001": true, "F002": false, "F003": false}'; / / the attribute name must be enclosed in quotation marks, otherwise the conversion will fail. Var obj=JSON.parse (str); console.log (obj.F002); / / false

Object to string:

Var obj= {F001 JSON.stringify obj) F002 JSON.stringify false}; / / Node native support JSON// {"F001": true, "F002": false, "F003": false} Thank you for reading this article carefully. I hope the article "nodejs how to convert a string into an object" shared by the editor is helpful to you. At the same time, I also hope you can support us, pay attention to the industry information channel, and more related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report