I have a JSON-formatted document like so:
{
"the-field": "something",
// etc
}
When I call foo = JSON.parse()
it spits out an object literal with a field foo.the-field
, but when I try console.log(foo.the-field)
I'm told that it's not proper formatting for a JavaScript variable. What gives?
You need to use the bracket notation instead of dot notation as the member operator here
foo["the-field"]
From Docs
If you are using dot notation then
property must be a valid JavaScript identifier, ie a sequence of alphanumerical characters, also including the underscore ("_") and dollar sign ("$"), that cannot start with a number. For example, object.$1 is valid, while object.1 is not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.