// 示例代码:处理 JSON 数组
// 定义一个 JSON 数组
const jsonArray = [
{ "name": "Alice", "age": 25, "city": "New York" },
{ "name": "Bob", "age": 30, "city": "Los Angeles" },
{ "name": "Charlie", "age": 35, "city": "Chicago" }
];
// 遍历 JSON 数组并输出每个对象的信息
jsonArray.forEach(person => {
console.log(`Name: ${person.name}, Age: ${person.age}, City: ${person.city}`);
});
// 将 JSON 数组转换为字符串
const jsonString = JSON.stringify(jsonArray);
console.log("JSON String:", jsonString);
// 将 JSON 字符串转换回数组
const parsedJsonArray = JSON.parse(jsonString);
console.log("Parsed JSON Array:", parsedJsonArray);
name
、age
和 city
属性。forEach
方法遍历数组中的每个对象,并通过模板字符串输出每个对象的属性值。JSON.stringify
将 JSON 数组转换为字符串格式。JSON.parse
将 JSON 字符串解析回数组格式。上一篇:js 数组随机取出一个
下一篇:js 数组转json
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站