gpt4 book ai didi

javascript - 如何访问多维 JSON 数据?

转载 作者:行者123 更新时间:2023-12-01 02:05:16 27 4
gpt4 key购买 nike

我正在尝试访问 JSON 文件的某些字段的值。

console.log(objects.assignments.header.report_type);

我想打印HOMEWORK

<小时/>

Javascript

$.ajax({

url: "/BIM/rest/report/assignment",
type: "POST",
dataType : "json",
data: {
assessmentId: "206a9246-ce83-412b-b8ad-6b3e28be44e3",
classroomId: "722bfadb-9774-4d59-9a47-89ac9a7a8f9a"
},

success: function( objects ) {

console.log( objects.assignments.header.report_type );

// Result : Uncaught TypeError: Cannot read property 'report_type' of undefined
<小时/>

JSON 数据 - 我从 AJAX 调用中返回

{
"assignments": [
{
"assignmentId": "SUMMARY",
"name": "Summary",
"header": {
"section_num": "9.4",
"report_type": "HOMEWORK",
"problem_set": "Summary",
"start_time": null,
"student_am": 0,
"student_total": 5,
"due_time": null,
"submit_am": 0,
"submit_total": 0,
"avg_score": "0.0",
"danger": 0,
"danger_list": "",
"warning": 0,
"warning_list": "",
"success": 0,
"success_list": ""
}
}
]
}
<小时/>

如何正确访问这些数据?

任何提示/帮助对我来说都意义重大。

提前致谢。

最佳答案

assignments 是一个数组,因此您需要访问特定元素。

console.log(objects.assignments[0].header.report_type);
^^^

关于javascript - 如何访问多维 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31384001/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com