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 to convert string into json object in JavaScript

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to convert a string into a json object in JavaScript". The explanation in this article is simple and clear, and is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to convert a string into a json object in JavaScript".

Method 1: use the eval () function for conversion

The eval () function evaluates a string, which is executed once in eval (), and executes the JavaScript code in it.

Example:

Function strToJson (str) {var json = eval ('('+ str +')'); return json;}

Note: you need to wrap a pair of parentheses around json characters; do not use this method for ie8 (compatibility mode), ie7, and ie6.

Method 2: use the JSON.parse () method for conversion

Function strToJson (str) {return JSON.parse (str);}

Note: the interior must be written in standard json format; ie8 (compatibility mode), ie7, ie6 do not use this method.

Thank you for reading, the above is the content of "how to convert a string into a json object in JavaScript". After the study of this article, I believe you have a deeper understanding of how to convert a string into a json object in JavaScript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report