gpt4 book ai didi

javascript - 从对象中获取对象键值

转载 作者:行者123 更新时间:2023-12-02 14:17:38 26 4
gpt4 key购买 nike

嗨,在我使用 data.driver.id 之前,如果在使用 node.js 中的内存包之前工作正常,那么从对象获取值有一个问题,它让我得到像 1010 一样的结果,但现在它给我未定义

这是我的对象,包含所有详细信息

var data = { event: 2,
driver: '{"phonenumber":"123123","client":{"index":"103.104.34.250:59555","sock":{"_connecting":false,"_handle":{"fd":21,"writeQueueSize":0,"owner":"[Circular]","reading":true},"_readableState":{"highWaterMark":16384,"buffer":[],"length":0,"pipes":null,"pipesCount":0,"flowing":false,"ended":false,"endEmitted":false,"reading":true,"calledRead":true,"sync":false,"needReadable":true,"emittedReadable":false,"readableListening":false,"objectMode":false,"defaultEncoding":"utf8","ranOut":false,"awaitDrain":0,"readingMore":false,"decoder":null,"encoding":null},"readable":true,"domain":null,"_events":{},"_maxListeners":10,"_writableState":{"highWaterMark":16384,"objectMode":false,"needDrain":false,"ending":false,"ended":false,"finished":false,"decodeStrings":false,"defaultEncoding":"utf8","length":0,"writing":false,"sync":false,"bufferProcessing":false,"writecb":null,"writelen":0,"buffer":[],"errorEmitted":false},"writable":true,"allowHalfOpen":false,"onend":null,"destroyed":false,"bytesRead":24,"_bytesDispatched":2,"_pendingData":null,"_pendingEncoding":"","server":{"domain":null,"_events":{},"_maxListeners":10,"_connections":1,"connections":1,"_handle":{"fd":17,"writeQueueSize":0,"owner":"[Circular]"},"_usingSlaves":false,"_slaves":[],"allowHalfOpen":false,"_connectionKey":"4:0.0.0.0:25001"},"_peername":{"address":"103.240.34.250","family":"IPv4","port":59555},"_consuming":true},"_actualLength":0,"_msg":[2,114,116,101,115,116,64,103,109,97,105,108,46,99,111,109,124,114,97,118,105,110,97],"_written":0,"driver":"[Circular]"},"online":true,"onDuty":true,"latitude":0,"longitude":0,"vehicle":"Sedan","id":1056,"fullname":"test01 test01","pendingRides":{},"ride":null,"rpm":"0","licensePlate":"dri123","logintime":1470925503}' }

console.log(data.driver.id);

我添加了我的代码,所以我想获取与驱动程序相关的所有属性,例如 id、全名、登录时间、纬度、经度,所以请帮助我解决此错误

最佳答案

您需要先解析 JSON。像这样尝试一下:

var data = { event: 2,
driver: '{"phonenumber":"123123","client":{"index":"103.104.34.250:59555","sock":{"_connecting":false,"_handle":{"fd":21,"writeQueueSize":0,"owner":"[Circular]","reading":true},"_readableState":{"highWaterMark":16384,"buffer":[],"length":0,"pipes":null,"pipesCount":0,"flowing":false,"ended":false,"endEmitted":false,"reading":true,"calledRead":true,"sync":false,"needReadable":true,"emittedReadable":false,"readableListening":false,"objectMode":false,"defaultEncoding":"utf8","ranOut":false,"awaitDrain":0,"readingMore":false,"decoder":null,"encoding":null},"readable":true,"domain":null,"_events":{},"_maxListeners":10,"_writableState":{"highWaterMark":16384,"objectMode":false,"needDrain":false,"ending":false,"ended":false,"finished":false,"decodeStrings":false,"defaultEncoding":"utf8","length":0,"writing":false,"sync":false,"bufferProcessing":false,"writecb":null,"writelen":0,"buffer":[],"errorEmitted":false},"writable":true,"allowHalfOpen":false,"onend":null,"destroyed":false,"bytesRead":24,"_bytesDispatched":2,"_pendingData":null,"_pendingEncoding":"","server":{"domain":null,"_events":{},"_maxListeners":10,"_connections":1,"connections":1,"_handle":{"fd":17,"writeQueueSize":0,"owner":"[Circular]"},"_usingSlaves":false,"_slaves":[],"allowHalfOpen":false,"_connectionKey":"4:0.0.0.0:25001"},"_peername":{"address":"103.240.34.250","family":"IPv4","port":59555},"_consuming":true},"_actualLength":0,"_msg":[2,114,116,101,115,116,64,103,109,97,105,108,46,99,111,109,124,114,97,118,105,110,97],"_written":0,"driver":"[Circular]"},"online":true,"onDuty":true,"latitude":0,"longitude":0,"vehicle":"Sedan","id":1056,"fullname":"test01 test01","pendingRides":{},"ride":null,"rpm":"0","licensePlate":"dri123","logintime":1470925503}' }

console.log(JSON.parse(data.driver).id);

关于javascript - 从对象中获取对象键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38900024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com