掌握JSON数组的结构、语法和实际应用
JSON数组是一个有序的值集合,使用中括号[]包裹。数组中的值可以是任何JSON数据类型,包括其他数组和对象。
["苹果", "香蕉", "橙子", "葡萄"]
["元素1", "元素2", "元素3"]
[1, 2, 3, 4, 5]
["文本", 42, true, null]
["A", "B", "C"]
[
"字符串",
42,
true,
null,
{
"对象键": "对象值"
},
["嵌套", "数组"]
]
["苹果", "香蕉", "橙子"]
[1, 2, 3, 4, 5]
[] // 空数组
['苹果', '香蕉', '橙子'] // 使用单引号
["苹果", "香蕉", "橙子",] // 末尾逗号
["苹果", "香蕉" "橙子"] // 缺少逗号
["苹果", "香蕉", "橙子", "葡萄"]
[10, 20, 30, 40, 50]
["文本", 42, true, null]
[
{
"id": 1,
"name": "张三",
"age": 30
},
{
"id": 2,
"name": "李四",
"age": 25
},
{
"id": 3,
"name": "王五",
"age": 35
}
]
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
[
"苹果",
"香蕉",
"橙子",
"葡萄",
"芒果"
]
// 假设arr是解析后的JSON数组
const first = arr[0]; // "苹果"
const second = arr[1]; // "香蕉"
const last = arr[4]; // "芒果"
const length = arr.length; // 5
// for循环
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
// forEach方法
arr.forEach(item => {
console.log(item);
});
import json
# 假设json_string是JSON字符串
data = json.loads(json_string)
# 访问元素
first = data[0] # "苹果"
second = data[1] # "香蕉"
last = data[-1] # "芒果"
# 数组长度
length = len(data) # 5
# 遍历数组
for item in data:
print(item)
import org.json.JSONArray;
// 假设jsonString是JSON字符串
JSONArray arr = new JSONArray(jsonString);
// 访问元素
String first = arr.getString(0); // "苹果"
String second = arr.getString(1); // "香蕉"
String last = arr.getString(4); // "芒果"
// 数组长度
int length = arr.length(); // 5
// 遍历数组
for (int i = 0; i < arr.length(); i++) {
System.out.println(arr.getString(i));
}
["苹果", "香蕉", "橙子"]
// 添加到末尾
arr.push("新元素");
// 添加到开头
arr.unshift("新元素");
// 添加到指定位置
arr.splice(2, 0, "新元素");
// 直接赋值
arr[0] = "更新后的值";
// 使用splice方法
arr.splice(1, 1, "新值");
// 删除最后一个元素
arr.pop();
// 删除第一个元素
arr.shift();
// 删除指定位置元素
arr.splice(2, 1);
// 查找元素索引
const index = arr.indexOf("香蕉");
// 检查是否包含元素
const hasElement = arr.includes("苹果");
// 查找满足条件的元素
const found = arr.find(item => item === "橙子");
// map - 转换数组元素
const newArr = arr.map(item => item.toUpperCase());
// filter - 过滤数组元素
const filtered = arr.filter(item => item.length > 2);
// reduce - 累积计算
const total = numbers.reduce((sum, num) => sum + num, 0);
// slice - 提取子数组
const subArray = arr.slice(1, 3);
// concat - 合并数组
const combined = arr1.concat(arr2);
// join - 数组转字符串
const str = arr.join(", ");
{
"article": {
"title": "JSON教程",
"author": "张三",
"tags": ["JSON", "教程", "Web开发", "数据交换"]
}
}
{
"shoppingCart": {
"userId": "u123456789",
"items": [
{
"productId": "p001",
"name": "智能手机",
"price": 2999.99,
"quantity": 1
},
{
"productId": "p002",
"name": "手机壳",
"price": 49.99,
"quantity": 2
},
{
"productId": "p003",
"name": "屏幕保护膜",
"price": 19.99,
"quantity": 3
}
],
"total": 3129.94
}
}
{
"class": "三年级二班",
"students": [
{
"id": 1,
"name": "张三",
"scores": [95, 87, 92, 88, 90]
},
{
"id": 2,
"name": "李四",
"scores": [78, 85, 80, 92, 88]
},
{
"id": 3,
"name": "王五",
"scores": [92, 95, 89, 94, 91]
}
]
}
{
"todoList": [
{
"id": 1,
"task": "学习JSON数组",
"completed": true,
"priority": "high",
"dueDate": "2023-10-10"
},
{
"id": 2,
"task": "完成项目报告",
"completed": false,
"priority": "medium",
"dueDate": "2023-10-15"
},
{
"id": 3,
"task": "准备会议材料",
"completed": false,
"priority": "low",
"dueDate": "2023-10-20"
}
]
}
{
"route": {
"name": "北京到上海",
"waypoints": [
[116.4074, 39.9042], // 北京
[117.2008, 39.0842], // 天津
[114.4995, 38.1006], // 石家庄
[113.2644, 23.1291], // 广州
[121.4737, 31.2304] // 上海
]
}
}
| 错误类型 | 错误示例 | 正确写法 | 说明 |
|---|---|---|---|
| 末尾逗号 | ["A", "B", "C",] | ["A", "B", "C"] | 最后一个元素后不能有逗号 |
| 缺少逗号 | ["A" "B" "C"] | ["A", "B", "C"] | 元素必须用逗号分隔 |
| 使用单引号 | ['A', 'B', 'C'] | ["A", "B", "C"] | JSON只支持双引号 |
| 注释 | [/* 注释 */ "A", "B"] | ["A", "B"] | JSON不支持注释 |
| 未转义特殊字符 | ["路径: C:\Windows"] | ["路径: C:\\Windows"] | 反斜杠需要转义 |
| 尾随逗号(某些解析器允许) | {"items": ["A", "B",]} | {"items": ["A", "B"]} | 严格JSON标准不允许尾随逗号 |