javascript - 无法读取 Node.JS 中的嵌套 JSON 对象
问题描述
我有一个名为“数据”的 JSON 节点,它有一个 ID。我想存储该 id 的值,但我无法用我当前的语法这样做,而且我收到一个错误,说它是未定义的。
我应该如何更改我的代码以使其正常工作?
我的代码片段:
const transfer_ID = data.items.id;
console.log("This is the transfer tree obtained", data);
数据:
{ entity: 'collection',
count: 1,
items:
[ { id: 'trf_Ary1cWasdfGmHAc',
entity: 'transfer',
source: 'pay_ASrxasdfetwhAFv',
amount: 390000,
currency: 'INR',
amount_reversed: 0,
notes: [],
fees: 1151,
tax: 176,
on_hold: false,
on_hold_until: null,
recipient_settlement_id: null,
created_at: 1530208843 } ] }
解决方案
检查以下代码片段
var data ={ entity: 'collection',
count: 1,
items:
[ { id: 'trf_Ary1cWasdfGmHAc',
entity: 'transfer',
source: 'pay_ASrxasdfetwhAFv',
amount: 390000,
currency: 'INR',
amount_reversed: 0,
notes: [],
fees: 1151,
tax: 176,
on_hold: false,
on_hold_until: null,
recipient_settlement_id: null,
created_at: 1530208843 } ] }
var id = data.items[0].id;
console.log(id);
推荐阅读
- sql - 新 SQL 用户 - 我正在尝试提取查询中的最新记录。SQL 新手会计
- flutter - Flutter Stopped build for IOS --Error running pod install
- javascript - 字符串变得不可变,我什至不能从中删除空格或修改它
- rust - 可变地借用结构字段并将其传递给可变方法
- c# - 如何在C#中延迟执行任务中的部分代码
- java - 如何使用流程构建器使用其本机 gui 执行命令
- java - 如何使用@Procedure 注释在 Spring JPA 中映射 VARCHAR2 类型表?
- javascript - 如何控制音频播放图标而不是全部循环播放?
- nginx - NGINX 入口控制器、SSL 和 optional_no_ca
- python - 无法导入已安装的包