Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to convert string into json object in JavaScript

Shulou Source: shulou.com Published: 2022-05-31 10:38:15 09月23日 Update

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!

Tags: Characters strings objects methods learning content functions patterns this method code packages instances that is ideas situations parentheses articles more standards formats Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Apple NVidia Shulou Technology Huawei