When working with JSON objects, accessing properties with dash characters can be problematic, resulting in errors like "ReferenceError: is not defined." This article explores the issue presented by the OP and provides a solution.
The issue arises when attempting to access a JSON property containing a dash using the dot notation, such as jsonObj.profile-id. Unfortunately, in JavaScript, this syntax is interpreted as a subtraction expression, leading to errors.
To resolve this issue, the square bracket notation should be employed instead:
jsonObj["profile-id"]
This approach treats the property name as a string literal, allowing access to values with special characters. By using the square bracket notation, developers can effectively retrieve data from JSON objects regardless of the characters present in the property names.
The above is the detailed content of How Do I Access JSON Properties with Dashes in Their Names?. For more information, please follow other related articles on the PHP Chinese website!