2

I've got an XML-file. After converting it to JSON I want to access some content within. This was possible. However, some variable within the JSON contain a - (minus sign). When I try to access it, Javascript interpret this as a calculation. Is the only way to workaround this to replace all the - signs?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
user6182078
  • 55
  • 1
  • 4

2 Answers2

2

You can use brackets notation:

yourJson['ab-cd']; // access to 'ab-cd' property that contains '-' sign
isvforall
  • 8,768
  • 6
  • 35
  • 50
2

If you want to define or access properties with special characters in them, you need to use string property names:

var obj = {
  'some-string-with-hyphens': true,
  'another-one': true
};

var another = obj['another-one'];
scott113341
  • 109
  • 4