将下述{ "class": "1A", "teacher": "Mr. Smith", "students": [ { "name": "Alice", "age": 12, "gender": "female" }, { "name": "Bob", "age": 13, "gender": "male" } ] }json串中的students数组转换成对象并打印对象
时间: 2023-07-03 15:27:06 浏览: 107
Json数据转换成对象
好的,您可以使用以下JavaScript代码将JSON字符串解析为JavaScript对象,然后打印出students数组对应的对象:
```javascript
const jsonString = '{ "class": "1A", "teacher": "Mr. Smith", "students": [ { "name": "Alice", "age": 12, "gender": "female" }, { "name": "Bob", "age": 13, "gender": "male" } ] }';
const jsonObject = JSON.parse(jsonString);
const studentsObject = jsonObject.students;
console.log(studentsObject);
```
运行结果如下:
```
[
{ name: 'Alice', age: 12, gender: 'female' },
{ name: 'Bob', age: 13, gender: 'male' }
]
```
其中,studentsObject就是JSON中students数组对应的JavaScript对象。
阅读全文