-2

I have a rather strange question. I have a json object with an @ character in its attribute name, like for example the na@me. I can't call on it since I can't make use of an @ character in the object.

Well change the character is perhaps your most obvious answer, but I wonder if there is another solution besides this? How I can call the na@me attribute?

<!DOCTYPE html>
<html>
    <body>
        <h2>JSON Object Creation in JavaScript</h2>
        <p id="demo"></p>
        <script>
            var text = '{"na@me":"John Johnson","street":"Oslo West 16","phone":"555 1234567"}'
            var obj = JSON.parse(text);
            document.getElementById("demo").innerHTML =
                obj.na@me + "<br>" +
                obj.street + "<br>" +
                obj.phone;
        </script>
    </body>
</html>
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
anders
  • 1,535
  • 5
  • 19
  • 43

1 Answers1

1
obj['na@me']

Bracket notation to the rescue.

You can only use dot notation for regular identifiers.

Scimonster
  • 32,893
  • 9
  • 77
  • 89