繁体   English   中英

如何从NodeJS中的Json对象列表中检索特定值

[英]How to retrieve specific value from list of Json Object in NodeJS

我正在尝试使用以下代码来检索executionArn,但出现此错误

Error [SyntaxError]: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)

如何从每条记录中获取executionArnstateMachineArn 任何帮助将非常感激。

console.log(data) - 输出

{
  executions: [
    {
      executionArn: 'arn:aws:states:us-east-2:12222:execution:test:dcb689bc',
      stateMachineArn: 'arn:aws:states:us-east-2:12222:stateMachine:test-sm',
      name: 'test-name',
      status: 'SUCCEEDED',
      startDate: 2021-06-17T13:43:39.817Z,
      stopDate: 2021-06-17T13:43:53.667Z
    },
    {
      executionArn: 'arn:aws:states:us-east-2:12222:execution:test:sd32dsdf',
      stateMachineArn: 'arn:aws:states:us-east-2:12222:stateMachine:test-sm',
      name: 'test-name1',
      status: 'SUCCEEDED',
      startDate: 2021-06-17T13:43:39.817Z,
      stopDate: 2021-06-17T13:43:53.667Z
    }
  ],
  nextToken: 'aadfdfdf'
}

代码:

  stepfunctions.listExecutions(params, function(err, data) {
    if (err) console.log(err, err.stack); 
    else     
    console.log(data)
    //console.log(data.executions[0].executionArn)
    data = JSON.parse(data);
    data.forEach(function(result) {
        var arnValue = result.executions.executionArn;
        console.log(arnValue);
    });

  });

数据是一个对象,其中的执行是一个数组,所以试试这个

data.executions.foreach(function (execution) {
  console.log('executionArn', execution.executionArn)
  console.log('stateMachineArn', execution.stateMachineArn)
})
executions.map(e=>{
   // access it here
   let a =  e.executionArn;
 });

提供的输出(数据)不是有效的 JSON 对象,

{...
  startDate: 2021-06-17T13:43:39.817Z,
  stopDate: 2021-06-17T13:43:53.667Z
}

对于有效的 JSON,它应该看起来像

{...
  "startDate": "2021-06-17T13:43:39.817Z",
  "stopDate": "2021-06-17T13:43:53.667Z"
}

以便正确迭代。

如果数据来自服务器(API),则在返回之前将日期字符串化,并且可以通过以下方式找到值

  data.executions.map(execution=>{
      let arnValue = execution.executionArn;
      console.log(arnValue);
  })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM