Convert string to object array javascript

王林
Release: 2023-05-12 19:51:36
Original
3086 people have browsed it

In JavaScript, strings and objects are two different types, and the data they store is also very different. In web development, we often encounter situations where we need to convert strings into object arrays, such as JSON data obtained from the server. This article will introduce several methods to convert strings into object arrays.

Method 1: Use JSON.parse()

In JavaScript, you can use the JSON.parse() method to convert a string in JSON format into an object or array. The principle of the JSON.parse() method is to parse a JSON string into a JavaScript object or array.

For example, we have the following JSON string:

var jsonString = '[{"name":"John","age":30,"city":"New York"},{"name":"Jane","age":25,"city":"Los Angeles"},{"name":"Bob","age":40,"city":"Chicago"}]';
Copy after login
Copy after login
Copy after login

Use the JSON.parse() method to convert it into an array of objects:

var jsonObjectArray = JSON.parse(jsonString);
Copy after login

We can use a loop statement to iterate over the Array, output the attributes of each object:

for(var i=0; i
Copy after login
Copy after login
Copy after login

Method 2: Use eval()

eval() is a function in JavaScript that can treat the incoming string parameters as JavaScript The code is executed. Therefore, we can use the eval() function to convert a JSON-formatted string into an object or array.

For example, we have the following JSON string:

var jsonString = '[{"name":"John","age":30,"city":"New York"},{"name":"Jane","age":25,"city":"Los Angeles"},{"name":"Bob","age":40,"city":"Chicago"}]';
Copy after login
Copy after login
Copy after login

Use the eval() function to convert it into an array of objects:

var jsonObjectArray = eval("("+jsonString+")");
Copy after login

Similarly, we can use a loop statement to iterate over the Array, output the attributes of each object:

for(var i=0; i
Copy after login
Copy after login
Copy after login

It should be noted that the eval() function may have security risks and is not recommended for use in a production environment. Instead, the JSON.parse() method should be used.

Method 3: Use string splitting and replacement

An older method is to use string splitting and replacement to convert the string into an object array.

For example, we have the following JSON string:

var jsonString = '[{"name":"John","age":30,"city":"New York"},{"name":"Jane","age":25,"city":"Los Angeles"},{"name":"Bob","age":40,"city":"Chicago"}]';
Copy after login
Copy after login
Copy after login

First, we need to replace the commas used to separate objects in the string with semicolons, and replace the curly brackets with square brackets, to get A legal JavaScript array declaration:

jsonString = jsonString.replace(/}/g, '},');
jsonString = jsonString.replace(/,$/, '');
jsonString = jsonString.replace(/{/g, '[');
jsonString = jsonString.replace(/}/g, '}]');
Copy after login

Then, use the eval() function to convert it into an object array:

var jsonObjectArray = eval(jsonString);
Copy after login

Similarly, we can use a loop statement to traverse the array and output each object Attributes:

for(var i=0; i
Copy after login
Copy after login
Copy after login

It should be noted that this method is only suitable for relatively simple JSON formats. For deeply nested JSON formats, this method will fail.

Summary

The above are several methods of converting strings into object arrays. In actual development, it is recommended to use the JSON.parse() method to convert JSON-formatted strings into objects or arrays. It is highly efficient and safe, and is more in line with the specifications of the language itself. If you need to support older browser versions, consider using the eval() function or string splitting and replacing.

The above is the detailed content of Convert string to object array javascript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!