Javascript My node.json解析函数抛出错误

Javascript My node.json解析函数抛出错误,javascript,node.js,json,Javascript,Node.js,Json,[JAVASCRIPT新手] 嗨 我试图解析json数据以提取不同的值,但我遇到了错误。你能帮忙吗 JSON数据 原始JSON数据样本: {"records":{"expiryDates":["03-Jun-2021","10-Jun-2021","17-Jun-2021","24-Jun-2021","01-Jul-2021","08-Jul-

[JAVASCRIPT新手]

我试图解析json数据以提取不同的值,但我遇到了错误。你能帮忙吗

JSON数据

原始JSON数据样本:

{"records":{"expiryDates":["03-Jun-2021","10-Jun-2021","17-Jun-2021","24-Jun-2021","01-Jul-2021","08-Jul-2021","15-Jul-2021","22-Jul-2021","29-Jul-2021","26-Aug-2021","30-Sep-2021","30-Dec-2021"],"data":[{"strikePrice":18000,"expiryDate":"30-Sep-2021","PE":{"strikePrice":18000,"expiryDate":"30-Sep-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY30-09-2021PE18000.00","openInterest":1,"changeinOpenInterest":0,"pchangeinOpenInterest":0,"totalTradedVolume":0,"impliedVolatility":0,"lastPrice":0.25,"change":0,"pChange":0,"totalBuyQuantity":23100,"totalSellQuantity":0,"bidQty":10000,"bidprice":5,"askQty":0,"askPrice":0,"underlyingValue":35141.45}},{"strikePrice":23000,"expiryDate":"24-Jun-2021","PE":{"strikePrice":23000,"expiryDate":"24-Jun-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY24-06-2021PE23000.00","openInterest":534,"changeinOpenInterest":-6,"pchangeinOpenInterest":-1.1111111111111112,"totalTradedVolume":82,"impliedVolatility":63.23,"lastPrice":9.5,"change":-2.5,"pChange":-20.833333333333336,"totalBuyQuantity":2775,"totalSellQuantity":875,"bidQty":50,"bidprice":9.5,"askQty":50,"askPrice":9.95,"underlyingValue":35141.45}},{"strikePrice":24000,"expiryDate":"30-Sep-2021","PE":{"strikePrice":24000,"expiryDate":"30-Sep-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY30-09-2021PE24000.00","openInterest":0,"changeinOpenInterest":0,"pchangeinOpenInterest":0,"totalTradedVolume":0,"impliedVolatility":0,"lastPrice":0,"change":0,"pChange":0,"totalBuyQuantity":20575,"totalSellQuantity":0,"bidQty":5000,"bidprice":5,"askQty":0,"askPrice":0,"underlyingValue":35141.45}},{"strikePrice":24000,"expiryDate":"30-Dec-2021","PE":{"strikePrice":24000,"expiryDate":"30-Dec-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY30-12-2021PE24000.00","openInterest":11,"changeinOpenInterest":0,"pchangeinOpenInterest":0,"totalTradedVolume":0,"impliedVolatility":0,"lastPrice":180,"change":0,"pChange":0,"totalBuyQuantity":100,"totalSellQuantity":0,"bidQty":50,"bidprice":151.55,"askQty":0,"askPrice":0,"underlyingValue":35141.45}},{"strikePrice":24000,"expiryDate":"24-Jun-2021","CE":{"strikePrice":24000,"expiryDate":"24-Jun-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY24-06-2021CE24000.00","openInterest":1,"changeinOpenInterest":0,"pchangeinOpenInterest":0,"totalTradedVolume":0,"impliedVolatility":0,"lastPrice":9223.8,"change":0,"pChange":0,"totalBuyQuantity":3650,"totalSellQuantity":2900,"bidQty":750,"bidprice":11073.95,"askQty":500,"askPrice":11544.4,"underlyingValue":35141.45},"PE":
async function formatDataAndStore(resp){
    return new Promise((resolve,reject)=>{
    //let myDate = new Date();
    //let currentTime = myDate.getHours()*100 + Math.floor(myDate.getMinutes()/5)*5;

    (resp.records.data).forEach(element => {
        console.log(JSON.parse(element.PE));
    });

    })
}
> (node:12578) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected
> token o in JSON at position 1
>     at JSON.parse (<anonymous>)
>     at /Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:79:26
>     at Array.forEach (<anonymous>)
>     at /Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:78:25
>     at new Promise (<anonymous>)
>     at formatDataAndStore (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:74:12)
>     at Query.<anonymous> (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:55:15)
>     at Query.<anonymous> (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/node_modules/mysql/lib/Connection.js:526:10)
>     at Query._callback (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/node_modules/mysql/lib/Connection.js:488:16)
>     at Query.Sequence.end (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
> (Use `node --trace-warnings ...` to show where the warning was
> created) (node:12578) UnhandledPromiseRejectionWarning: Unhandled
> promise rejection. This error originated either by throwing inside of
> an async function without a catch block, or by rejecting a promise
> which was not handled with .catch(). To terminate the node process on
> unhandled promise rejection, use the CLI flag
> `--unhandled-rejections=strict` (see
> https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).
> (rejection id: 2) (node:12578) [DEP0018] DeprecationWarning: Unhandled
> promise rejections are deprecated. In the future, promise rejections
> that are not handled will terminate the Node.js process with a
> non-zero exit code.
My node.js代码:

{"records":{"expiryDates":["03-Jun-2021","10-Jun-2021","17-Jun-2021","24-Jun-2021","01-Jul-2021","08-Jul-2021","15-Jul-2021","22-Jul-2021","29-Jul-2021","26-Aug-2021","30-Sep-2021","30-Dec-2021"],"data":[{"strikePrice":18000,"expiryDate":"30-Sep-2021","PE":{"strikePrice":18000,"expiryDate":"30-Sep-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY30-09-2021PE18000.00","openInterest":1,"changeinOpenInterest":0,"pchangeinOpenInterest":0,"totalTradedVolume":0,"impliedVolatility":0,"lastPrice":0.25,"change":0,"pChange":0,"totalBuyQuantity":23100,"totalSellQuantity":0,"bidQty":10000,"bidprice":5,"askQty":0,"askPrice":0,"underlyingValue":35141.45}},{"strikePrice":23000,"expiryDate":"24-Jun-2021","PE":{"strikePrice":23000,"expiryDate":"24-Jun-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY24-06-2021PE23000.00","openInterest":534,"changeinOpenInterest":-6,"pchangeinOpenInterest":-1.1111111111111112,"totalTradedVolume":82,"impliedVolatility":63.23,"lastPrice":9.5,"change":-2.5,"pChange":-20.833333333333336,"totalBuyQuantity":2775,"totalSellQuantity":875,"bidQty":50,"bidprice":9.5,"askQty":50,"askPrice":9.95,"underlyingValue":35141.45}},{"strikePrice":24000,"expiryDate":"30-Sep-2021","PE":{"strikePrice":24000,"expiryDate":"30-Sep-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY30-09-2021PE24000.00","openInterest":0,"changeinOpenInterest":0,"pchangeinOpenInterest":0,"totalTradedVolume":0,"impliedVolatility":0,"lastPrice":0,"change":0,"pChange":0,"totalBuyQuantity":20575,"totalSellQuantity":0,"bidQty":5000,"bidprice":5,"askQty":0,"askPrice":0,"underlyingValue":35141.45}},{"strikePrice":24000,"expiryDate":"30-Dec-2021","PE":{"strikePrice":24000,"expiryDate":"30-Dec-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY30-12-2021PE24000.00","openInterest":11,"changeinOpenInterest":0,"pchangeinOpenInterest":0,"totalTradedVolume":0,"impliedVolatility":0,"lastPrice":180,"change":0,"pChange":0,"totalBuyQuantity":100,"totalSellQuantity":0,"bidQty":50,"bidprice":151.55,"askQty":0,"askPrice":0,"underlyingValue":35141.45}},{"strikePrice":24000,"expiryDate":"24-Jun-2021","CE":{"strikePrice":24000,"expiryDate":"24-Jun-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY24-06-2021CE24000.00","openInterest":1,"changeinOpenInterest":0,"pchangeinOpenInterest":0,"totalTradedVolume":0,"impliedVolatility":0,"lastPrice":9223.8,"change":0,"pChange":0,"totalBuyQuantity":3650,"totalSellQuantity":2900,"bidQty":750,"bidprice":11073.95,"askQty":500,"askPrice":11544.4,"underlyingValue":35141.45},"PE":
async function formatDataAndStore(resp){
    return new Promise((resolve,reject)=>{
    //let myDate = new Date();
    //let currentTime = myDate.getHours()*100 + Math.floor(myDate.getMinutes()/5)*5;

    (resp.records.data).forEach(element => {
        console.log(JSON.parse(element.PE));
    });

    })
}
> (node:12578) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected
> token o in JSON at position 1
>     at JSON.parse (<anonymous>)
>     at /Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:79:26
>     at Array.forEach (<anonymous>)
>     at /Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:78:25
>     at new Promise (<anonymous>)
>     at formatDataAndStore (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:74:12)
>     at Query.<anonymous> (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:55:15)
>     at Query.<anonymous> (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/node_modules/mysql/lib/Connection.js:526:10)
>     at Query._callback (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/node_modules/mysql/lib/Connection.js:488:16)
>     at Query.Sequence.end (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
> (Use `node --trace-warnings ...` to show where the warning was
> created) (node:12578) UnhandledPromiseRejectionWarning: Unhandled
> promise rejection. This error originated either by throwing inside of
> an async function without a catch block, or by rejecting a promise
> which was not handled with .catch(). To terminate the node process on
> unhandled promise rejection, use the CLI flag
> `--unhandled-rejections=strict` (see
> https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).
> (rejection id: 2) (node:12578) [DEP0018] DeprecationWarning: Unhandled
> promise rejections are deprecated. In the future, promise rejections
> that are not handled will terminate the Node.js process with a
> non-zero exit code.
我收到的错误:

{"records":{"expiryDates":["03-Jun-2021","10-Jun-2021","17-Jun-2021","24-Jun-2021","01-Jul-2021","08-Jul-2021","15-Jul-2021","22-Jul-2021","29-Jul-2021","26-Aug-2021","30-Sep-2021","30-Dec-2021"],"data":[{"strikePrice":18000,"expiryDate":"30-Sep-2021","PE":{"strikePrice":18000,"expiryDate":"30-Sep-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY30-09-2021PE18000.00","openInterest":1,"changeinOpenInterest":0,"pchangeinOpenInterest":0,"totalTradedVolume":0,"impliedVolatility":0,"lastPrice":0.25,"change":0,"pChange":0,"totalBuyQuantity":23100,"totalSellQuantity":0,"bidQty":10000,"bidprice":5,"askQty":0,"askPrice":0,"underlyingValue":35141.45}},{"strikePrice":23000,"expiryDate":"24-Jun-2021","PE":{"strikePrice":23000,"expiryDate":"24-Jun-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY24-06-2021PE23000.00","openInterest":534,"changeinOpenInterest":-6,"pchangeinOpenInterest":-1.1111111111111112,"totalTradedVolume":82,"impliedVolatility":63.23,"lastPrice":9.5,"change":-2.5,"pChange":-20.833333333333336,"totalBuyQuantity":2775,"totalSellQuantity":875,"bidQty":50,"bidprice":9.5,"askQty":50,"askPrice":9.95,"underlyingValue":35141.45}},{"strikePrice":24000,"expiryDate":"30-Sep-2021","PE":{"strikePrice":24000,"expiryDate":"30-Sep-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY30-09-2021PE24000.00","openInterest":0,"changeinOpenInterest":0,"pchangeinOpenInterest":0,"totalTradedVolume":0,"impliedVolatility":0,"lastPrice":0,"change":0,"pChange":0,"totalBuyQuantity":20575,"totalSellQuantity":0,"bidQty":5000,"bidprice":5,"askQty":0,"askPrice":0,"underlyingValue":35141.45}},{"strikePrice":24000,"expiryDate":"30-Dec-2021","PE":{"strikePrice":24000,"expiryDate":"30-Dec-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY30-12-2021PE24000.00","openInterest":11,"changeinOpenInterest":0,"pchangeinOpenInterest":0,"totalTradedVolume":0,"impliedVolatility":0,"lastPrice":180,"change":0,"pChange":0,"totalBuyQuantity":100,"totalSellQuantity":0,"bidQty":50,"bidprice":151.55,"askQty":0,"askPrice":0,"underlyingValue":35141.45}},{"strikePrice":24000,"expiryDate":"24-Jun-2021","CE":{"strikePrice":24000,"expiryDate":"24-Jun-2021","underlying":"BANKNIFTY","identifier":"OPTIDXBANKNIFTY24-06-2021CE24000.00","openInterest":1,"changeinOpenInterest":0,"pchangeinOpenInterest":0,"totalTradedVolume":0,"impliedVolatility":0,"lastPrice":9223.8,"change":0,"pChange":0,"totalBuyQuantity":3650,"totalSellQuantity":2900,"bidQty":750,"bidprice":11073.95,"askQty":500,"askPrice":11544.4,"underlyingValue":35141.45},"PE":
async function formatDataAndStore(resp){
    return new Promise((resolve,reject)=>{
    //let myDate = new Date();
    //let currentTime = myDate.getHours()*100 + Math.floor(myDate.getMinutes()/5)*5;

    (resp.records.data).forEach(element => {
        console.log(JSON.parse(element.PE));
    });

    })
}
> (node:12578) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected
> token o in JSON at position 1
>     at JSON.parse (<anonymous>)
>     at /Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:79:26
>     at Array.forEach (<anonymous>)
>     at /Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:78:25
>     at new Promise (<anonymous>)
>     at formatDataAndStore (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:74:12)
>     at Query.<anonymous> (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:55:15)
>     at Query.<anonymous> (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/node_modules/mysql/lib/Connection.js:526:10)
>     at Query._callback (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/node_modules/mysql/lib/Connection.js:488:16)
>     at Query.Sequence.end (/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
> (Use `node --trace-warnings ...` to show where the warning was
> created) (node:12578) UnhandledPromiseRejectionWarning: Unhandled
> promise rejection. This error originated either by throwing inside of
> an async function without a catch block, or by rejecting a promise
> which was not handled with .catch(). To terminate the node process on
> unhandled promise rejection, use the CLI flag
> `--unhandled-rejections=strict` (see
> https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).
> (rejection id: 2) (node:12578) [DEP0018] DeprecationWarning: Unhandled
> promise rejections are deprecated. In the future, promise rejections
> that are not handled will terminate the Node.js process with a
> non-zero exit code.
>(节点:12578)未处理的PromisejectionWarning:语法错误:意外
>位置1处JSON中的标记o
>在JSON.parse()处
>在/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:79:26
>在Array.forEach()处
>在/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:78:25
>在新的承诺()
>在formatDataAndStore(/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:74:12)
>在询问时。(/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/server.js:55:15)
>在询问时。(/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/node_modules/mysql/lib/Connection.js:526:10)
>在Query.\u回调(/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/node\u modules/mysql/lib/Connection.js:488:16)
>在Query.Sequence.end(/Users/manjulamkumar/Desktop/myRepo/DEV/OptionChain/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
>(使用`node--trace warnings…`显示警告的位置。)
>已创建)(节点:12578)未处理PromisejectionWarning:未处理
>承诺拒绝。此错误源于
>没有catch块或拒绝承诺的异步函数
>这不是用.catch()处理的。要终止上的节点进程,请执行以下操作:
>未处理的承诺拒绝,请使用CLI标志
>`--未处理的拒绝=严格`(请参阅
> https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode).
>(拒绝id:2)(节点:12578)[DEP0018]拒绝警告:未处理
>拒绝承诺是不推荐的。在未来,承诺拒绝
>未处理的将使用
>非零退出代码。

它已经被解析了,如果没有,你怎么做resp.records.data?因此,我建议你做一些类似的事情

resp.records.data.map(e => console.log(JSON.stringify(e.PE)))


如果从日志行中删除
JSON.parse
,会记录什么?它已经是json了吗?但是当我记录element.strikePrice时,仍然会得到相同的错误,它可以工作,但无法在PE内部读取。