Saturday 15 February 2014

javascript - Acessing JSON Information by order -



javascript - Acessing JSON Information by order -

i reading info json string, , when execute console.log(record.seentime.time.seenepoch) display right information.

but when utilize console.log(record.seentime.time[0].seenepoch), error saying :

typeerror: cannot read property 'seenepoch' of undefined.

here illustration set of data:

{ seentimes: { time: { seentime: '2014-09-10t20:18:32z', seenepoch: 1410380312 } } }

anyone know doing wrong? thanks

record.seentimes in case object, not array, can verify using

typeof record.seentimes

because of that, time[0] returns undefined.

seenepoch property of 1 , time object, therefore, access using record.seentimes.time.seenepoch

for once, i'll recommend reading w3schools : json syntax

it'll show examples of can stored in json.

edit :

your sample record.seentimes not able store multiple time objects, uses curly brackets {} indicate it's meant store object , if want able store multiple time objects, ie: array, json have :

record { seentimes: [ { time: { seentime: '2014-09-10t20:18:32z', seenepoch: 1410380312 } }, { time: { seentime: '2014-09-10t20:18:32z', seenepoch: 1410380312 } } ] }

note square brackets seentime holds array.

and slebetman noted :

also note in javascript, object defined having multiple identical keys technically invalid. implementations take lastly definition. example, object: {a:1,a:2,a:3} same {a:3}. if don't utilize array there 1 time object if appears twice in json string.

javascript json node.js

No comments:

Post a Comment