Accessing JSON object keys having spaces
javascript, json
Solution
The way to do this is via the bracket notation.
var test = {
"id": "109",
"No. of interfaces": "4"
}
alert(test["No. of interfaces"]);
For more info read out here:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
Problem
Let's say I have a following object: ``` { "id": "109", "No. of interfaces": "4" } ``` Following works fine for the "id" key: ``` alert(obj.id); // Works fine! ``` But if keys have spaces, then I cannot access their values using the dot notation e.g. ``` alert(obj."No. of interfaces"); // Syntax error ``` How can I access values, whose key names have spaces using the dot notation? Is it even possible with the dot notation? Or do I have to use another way?