遍历嵌套JSON返回NULL

kulphzqa  于 2022-12-05  发布在  其他
关注(0)|答案(4)|浏览(157)

我试图更好地理解如何在JavaScript/React中处理嵌套的JSON对象。我通过GitLab API以如下形式获取数据:

const merge_requests =    [
      {
        "id": 39329289,
        "iid": 156,
        "project_id": 231,
        "title": "Repaired some Links",
        "description": "",
        "state": "merged",
        "created_at": "2022-12-03T12:22:14.690Z",
        "updated_at": "2022-12-03T12:22:20.060Z",
        "merged_by": {
          "id": 1000,
          "username": "test.user",
          "name": "test.user@gmail.de",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merge_user": {
          "id": 2802,
          "username": "tes.user",
          "name": "test.user@gmail.de",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merged_at": "2022-12-03T12:22:20.072Z",
        "closed_by": null,
        "closed_at": null,
        "assignees": [],
        "assignee": null,
        "reviewers": [],
        "source_project_id": 231,
        "target_project_id": 231,
        "labels": [],
        "squash_commit_sha": null,
        "discussion_locked": null,
        "should_remove_source_branch": null,
        "force_remove_source_branch": null,
        "reference": "!156",
        "references": {
          "short": "!156",
          "relative": "!156",
          "full": ""
        },
        "web_url": "",
        "time_stats": {
          "time_estimate": 0,
          "total_time_spent": 0,
          "human_time_estimate": null,
          "human_total_time_spent": null
        },
        "squash": false,
        "task_completion_status": {
          "count": 0,
          "completed_count": 0
        },
        "has_conflicts": false,
        "blocking_discussions_resolved": true,
        "approvals_before_merge": null
      },
      {
        "id": 39329289,
        "iid": 156,
        "project_id": 231,
        "title": "Repaired some Links",
        "description": "",
        "state": "merged",
        "created_at": "2022-12-03T12:22:14.690Z",
        "updated_at": "2022-12-03T12:22:20.060Z",
        "merged_by": {
          "id": 1000,
          "username": "test.user",
          "name": "test.user@gmail.de",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merge_user": {
          "id": 2802,
          "username": "test.user",
          "name": "test.user@gmail.de",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merged_at": "2022-12-03T12:22:20.072Z",
        "closed_by": null,
        "closed_at": null,
        "assignees": [],
        "assignee": null,
        "reviewers": [],
        "source_project_id": 231,
        "target_project_id": 231,
        "labels": [],
        "squash_commit_sha": null,
        "discussion_locked": null,
        "should_remove_source_branch": null,
        "force_remove_source_branch": null,
        "reference": "!156",
        "references": {
          "short": "!156",
          "relative": "!156",
          "full": ""
        },
        "web_url": "",
        "time_stats": {
          "time_estimate": 0,
          "total_time_spent": 0,
          "human_time_estimate": null,
          "human_total_time_spent": null
        },
        "squash": false,
        "task_completion_status": {
          "count": 0,
          "completed_count": 0
        },
        "has_conflicts": false,
        "blocking_discussions_resolved": true,
        "approvals_before_merge": null
      },]

我想循环遍历这个JSON中的所有对象(合并请求),并使用merge_user. name创建一个新数组。

console.log(merge_requests[0].merge_user.name);
console.log(merge_requests[1].merge_user.name);

上面的日志返回了两个正确的值。但是,我不能循环通过JSON从数据创建一个新的数组,如下所示:

const arrTest = [];

  for(var i = 0; i < Object.keys(merge_requests).length; i++)
  {
      var mergeUserName = merge_requests[i].merge_user.name;
      arrTest.push(mergeUserName);
  }

  console.log(arrTest);
}

上面的代码会导致以下错误:Uncaught (in promise) TypeError: resultData[i].merge_user is null
下面是一张图片:

我目前正在学习来自R的JS。我在使用JSON而不是 Dataframe 时遇到了巨大的问题,我找不到任何可以学习的文档。我将感谢任何建议/来源。

k2arahey

k2arahey1#

不需要使用Object.keys(),可以直接使用merge_requests.length

const arrTest = [];

for(var i = 0; i < merge_requests.length; i++){
 let mergeUserName = merge_requests[i].merge_user.name;
 arrTest.push(mergeUserName);
}

console.log(arrTest);
const merge_requests =    [
      {
        "id": 39329289,
        "iid": 156,
        "project_id": 231,
        "title": "Repaired some Links",
        "description": "",
        "state": "merged",
        "created_at": "2022-12-03T12:22:14.690Z",
        "updated_at": "2022-12-03T12:22:20.060Z",
        "merged_by": {
          "id": 1000,
          "username": "test.user",
          "name": "test.user@gmail.de",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merge_user": {
          "id": 2802,
          "username": "tes.user",
          "name": "test.user@gmail.de",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merged_at": "2022-12-03T12:22:20.072Z",
        "closed_by": null,
        "closed_at": null,
        "assignees": [],
        "assignee": null,
        "reviewers": [],
        "source_project_id": 231,
        "target_project_id": 231,
        "labels": [],
        "squash_commit_sha": null,
        "discussion_locked": null,
        "should_remove_source_branch": null,
        "force_remove_source_branch": null,
        "reference": "!156",
        "references": {
          "short": "!156",
          "relative": "!156",
          "full": ""
        },
        "web_url": "",
        "time_stats": {
          "time_estimate": 0,
          "total_time_spent": 0,
          "human_time_estimate": null,
          "human_total_time_spent": null
        },
        "squash": false,
        "task_completion_status": {
          "count": 0,
          "completed_count": 0
        },
        "has_conflicts": false,
        "blocking_discussions_resolved": true,
        "approvals_before_merge": null
      },
      {
        "id": 39329289,
        "iid": 156,
        "project_id": 231,
        "title": "Repaired some Links",
        "description": "",
        "state": "merged",
        "created_at": "2022-12-03T12:22:14.690Z",
        "updated_at": "2022-12-03T12:22:20.060Z",
        "merged_by": {
          "id": 1000,
          "username": "test.user",
          "name": "test.user@gmail.de",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merge_user": {
          "id": 2802,
          "username": "test.user",
          "name": "test.user@gmail.de",
          "state": "active",
          "avatar_url": "",
          "web_url": ""
        },
        "merged_at": "2022-12-03T12:22:20.072Z",
        "closed_by": null,
        "closed_at": null,
        "assignees": [],
        "assignee": null,
        "reviewers": [],
        "source_project_id": 231,
        "target_project_id": 231,
        "labels": [],
        "squash_commit_sha": null,
        "discussion_locked": null,
        "should_remove_source_branch": null,
        "force_remove_source_branch": null,
        "reference": "!156",
        "references": {
          "short": "!156",
          "relative": "!156",
          "full": ""
        },
        "web_url": "",
        "time_stats": {
          "time_estimate": 0,
          "total_time_spent": 0,
          "human_time_estimate": null,
          "human_total_time_spent": null
        },
        "squash": false,
        "task_completion_status": {
          "count": 0,
          "completed_count": 0
        },
        "has_conflicts": false,
        "blocking_discussions_resolved": true,
        "approvals_before_merge": null
      }]
      
const arrTest = [];

for(var i = 0; i < merge_requests.length; i++){
 let mergeUserName = merge_requests[i].merge_user.name;
 arrTest.push(mergeUserName);
}

console.log(arrTest);
mm5n2pyu

mm5n2pyu2#

const arrTest = [];

for(var i = 0; i < merge_requests.length; i++){
 let mergeUserName = merge_requests[i].merge_user?.name;
 arrTest.push(mergeUserName);
}

console.log(arrTest);

如果json中不存在对象,merge_requests[i].merge_user?.name将返回undefined

h5qlskok

h5qlskok3#

我复制并粘贴了你的代码和JSON,它工作得很好。
确保从ate API获取JSON后对其进行解析typeof merge_requests应返回object,如果返回string,则执行以下操作:const parsedData = JSON.parse(merge_requests)和通过parsedData的循环

khbbv19g

khbbv19g4#

我检查了你的代码,运行正常.
检查您的api请求,您确定要等到它得到满足吗?

相关问题