I have created JSON by using the json_encode
PHP function. The key of one of the items of the array contains a forward slash and when the JSON is parsed, the object looks like this when output in Chrome's console.
Object
contact/allow_anonymous: "0"
menulayout: "horizontal"
pages/max_pages: "10"
primarycolour: "329e95"
websitelogo: "text"
My problem is that I can't seem to be able to access the value of the properties that have a forward slash in them.
Any ideas? Since javascript allowed me to create the object I would assume there is a way to retrieve the values.
Answers. You need to escape the forward slash in regular expression by adding a backslash infornt of it.
Object properties and methods can be accessed using dot notation or [ ] bracket.
Just use myObject["key"]
instead of myObject.key
:
alert(myObject["contact/allow_anonymous"]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With