ProAnswers.org

Can I access JSON objects by key in Javascript?

1 Like

No,You can’t access JSON objects by key in Javascript.

but you can Accessing Values in Javascript JSON Objects with Keys containing Hyphens (Dashes)



In Javascript, JSON objects (well, JS objects in general) have a great short hand little way of accessing values using the fullstop object notation.



In other words, using a JSON dataset looking like:




	{“status”:”1″,”message-data”:”Success!”}




we could access the “status” value by simply entering:




	alert(myobject.status);




However, if we tried the same thing with the “message-data” key, you will note that this will fall over syntactically straight away – all because of that silly hyphen (dash).