如何从Javascript中的JSON arry对象读取数据

时间:2019-01-15 03:30:54

标签: javascript arrays node.js json

我正在尝试从Javascript中的JSON数组获取值:

JSON数组如下:

[
   {
      "_id": 0,
      "_entityMetadataList": [
         {
            "_metadataValue": "/storage/emulated/0/DCIM/.thumbnails/1524101696032.jpg",
         },
         {
            "_metadataValue": "/storage/emulated/0/DCIM/.thumbnails/1524101694488.jpg",
         }
      ],
      "_timeCreated": "Tue Jan 15 06:10:04 2019\n",
      "_timeUpdated": "Tue Jan 15 06:10:04 2019\n",
      "objectEntity": {
         "_id": 0,
         "_EntitySiteGUID": -1
      }
   }
]

我该怎么做:

app.post('/sound', function (req, res) {
    let entitiesArray = req.body['filter'];

    console.log('entitiesArray: ' + JSON.stringify(entitiesArray._entityMetadataList[0]._metadataValue))

(顺便说一下,这是在Node环境中)

我,但是仍然出现错误:

TypeError: Cannot read property '0' of undefined  

3 个答案:

答案 0 :(得分:4)

似乎您还需要传递revEntitiesArray的索引。

尝试一下

console.log('revEntitiesArray: ' + 
JSON.stringify(revEntitiesArray[0]._revEntityMetadataList[0]._metadataValue))

答案 1 :(得分:1)

也许这段代码可以提供帮助

data = [
   {
      "_id": 0,
      "_revEntityMetadataList": [
         {
            "_metadataValue": "/storage/emulated/0/DCIM/.thumbnails/1524101696032.jpg",
         },
         {
            "_metadataValue": "/storage/emulated/0/DCIM/.thumbnails/1524101694488.jpg",
         }
      ],
      "_timeCreated": "Tue Jan 15 06:10:04 2019\n",
      "_timeUpdated": "Tue Jan 15 06:10:04 2019\n",
      "revObjectEntity": {
         "_id": 0,
         "_revEntitySiteGUID": -1
      }
   }
]`

data[0]["_revEntityMetadataList"][0]

答案 2 :(得分:0)

好像您错过了最外面的引用是一个数组

revEntitiesArray[0]._revEntityMetadataList[0]._metadataValue

如有疑问,请将路径表达式的每个部分分配给局部变量,然后逐步调试。