admin管理员组

文章数量:1208155

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.

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.

Share Improve this question edited Nov 25, 2018 at 17:41 trincot 350k36 gold badges271 silver badges322 bronze badges asked Apr 25, 2012 at 17:43 Gabriel SpiteriGabriel Spiteri 4,97812 gold badges44 silver badges60 bronze badges 2
  • 2 Have you tried myObject["contact/allow_anonymous"]? – Tejs Commented Apr 25, 2012 at 17:44
  • What did you write that didn't work? How was the object created? – Heitor Chang Commented Apr 25, 2012 at 17:49
Add a comment  | 

2 Answers 2

Reset to default 21

Just use myObject["key"] instead of myObject.key:

alert(myObject["contact/allow_anonymous"]);

Just replace the forward slash with ~1.

Instead of contact/allow_anonymous use contact~1allow_anonymous

本文标签: javascriptaccessing an object property that contains forward slashesStack Overflow