js json iter
时间: 2023-12-05 17:06:46 浏览: 30
您好!对于 JavaScript 中的 JSON 迭代,您可以使用以下方法之一:
1. 使用 for...in 循环迭代 JSON 对象的属性:
```javascript
const json = {
"name": "John",
"age": 30,
"city": "New York"
};
for (let key in json) {
console.log(key + ": " + json[key]);
}
```
2. 使用 Object.keys() 方法迭代 JSON 对象的属性:
```javascript
const json = {
"name": "John",
"age": 30,
"city": "New York"
};
Object.keys(json).forEach(key => {
console.log(key + ": " + json[key]);
});
```
3. 使用 Array.prototype.forEach() 方法迭代 JSON 数组:
```javascript
const jsonArray = [
{ "name": "John", "age": 30 },
{ "name": "Jane", "age": 25 },
{ "name": "Bob", "age": 35 }
];
jsonArray.forEach(item => {
console.log(item.name + ": " + item.age);
});
```
以上是几种常见的 JSON 迭代方法,您可以根据具体的需求选择适合的方法进行迭代。如果您有其他问题,请随时提问!