2016-08-11 17 views
0

こんにちは、私はdata.river.idを使用する前にnode.jsに保存されたパッケージで作業する前に、オブジェクトから値を取得することに問題が1つあります。私は、これはすべての詳細 オブジェクトからオブジェクトのキー値を取得する

私はID、フルネーム、logintime、緯度、経度などのドライバに関連するすべてのプロパティを取得したいので、私のコードを追加し

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);
と私のオブジェクト

ある未定義だから私を喜ばせてくださいeこのエラーが発生しました

+2

data.driverは文字列です。 – Redu

+1

...プロパティを読み取る前にJSON文字列を解析する必要があります。 – undefined

答えて

1

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

+0

ありがとうバディー細かいドライバIDを取得 –

1

誰かがオブジェクトに変換する文字列を解析示唆しました。しかし、それはすでにオブジェクトです。一重引用符を削除するだけです。

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

+0

どのようにコードを使用して削除することができます...任意のコードがありますので、ここに書き込んでください –

+0

@trusharwidetechできません。元のJavascriptを制御できない場合は、解析してください。 – Nate

関連する問題