开发者

Safari Javascript is conflicting JSON item with keyword

开发者 https://www.devze.com 2023-01-23 09:59 出处:网络
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

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"]);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号