首页 > 解决方案 > 无法读取 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 } ] }

标签: javascriptjsonnode.js

解决方案


检查以下代码片段

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);


推荐阅读