Safari Javascript is conflicting JSON item with keyword
I have the following JSON data:
{"data":{
"default":"some data"
}
}
Whenever I try to access data.default
, Safari generates sytax error because the keyword default
. Other web browsers seem to work.
What is th开发者_StackOverflow中文版e work around for this type of issue?
note: I can't change the JSON data. It is out of my control.
data["default"]
works exactly the same, but takes a string
Use data["default"]
instead. The word default, normally used within switch statements, is contained within a string, so the JavaScript parser does not complain about an "unexpected keyword."
This should work:
var js = {"data":{
"default":"some data"
}
};
alert(js.data["default"]);
精彩评论