I have the following json:
x = {
"body-json": {
"id": "1"
}
}
I'm trying to access the id this way:
console.log(x.body-json.id);
It turns out that I can not access the reference body-json
. Can anyone help me?
I have the following json:
x = {
"body-json": {
"id": "1"
}
}
I'm trying to access the id this way:
console.log(x.body-json.id);
It turns out that I can not access the reference body-json
. Can anyone help me?
You need to use the second syntax to access your object:
console.log(x["body-json"].id)
Since the expression x.body-json.id is interpreted as x.body - json.id
according to the JavaScript syntax rules. To access the variable you want, you need to use the syntax that passes the property name as a string.