创建Javascript对象时出错

Error creating a Javascript Object

本文关键字:出错 对象 Javascript 创建      更新时间:2023-09-26

下面这行代码,我认为它创建了一个javascript对象,但它给了我以下错误:

Uncaught SyntaxError: Unexpected token {

这是代码行:

    var flyer2 = {"id":20,"img_url":"http://images.awfbeat.com/48395b02-59e5-4e26-b7bc-8c603008c9c4","img_width":0,"img_height":0,"popularity":0,"color":"#67c547","tag":"Jul 10","category":"festivals","title":"Darth Vader'''u0027s '''"Annihilator'''"","title_slug":"darth-vader-s-annihilator-","performer":"","performer_sort":"xxxx","posted":"2012-03-11 04:09:20.0","facebook_event_id":"","venue_postal":"90802","venue_name":" Aquarium of the Pacific","venue_street":"100 Aquarium Way","venue_city":"Los Angeles","venue_region_abbr":"CA","venue_lat":"33.762226","venue_lng":"-118.19686","needs_info":false};

我到底做错了什么?

您正在对引号进行html编码。

不是100%确定,但我猜"与"符号被解析为按位和运算符,分号作为行尾符号。

如果这是服务器端代码,那么解决方案是:

 var flyer2 = "{"'""id"'"":20,"'""img_url"'"":"'""http://images.awfbeat.com/48395b02-59e5-4e26-b7bc-8c603008c9c4"'"","'""img_width"'"":0,"'""img_height"'"":0,"'""popularity"'"":0,"'""color"'"":"'""#67c547"'"","'""tag"'"":"'""Jul 10"'"","'""category"'"":"'""festivals"'"","'""title"'"":"'""Darth Vader'''u0027s '''"'""Annihilator'''"'"""'"","'""title_slug"'"":"'""darth-vader-s-annihilator-"'"","'""performer"'"":"'"""'"","'""performer_sort"'"":"'""xxxx"'"","'""posted"'"":"'""2012-03-11 04:09:20.0"'"","'""facebook_event_id"'"":"'"""'"","'""venue_postal"'"":"'""90802"'"","'""venue_name"'"":"'"" Aquarium of the Pacific"'"","'""venue_street"'"":"'""100 Aquarium Way"'"","'""venue_city"'"":"'""Los Angeles"'"","'""venue_region_abbr"'"":"'""CA"'"","'""venue_lat"'"":"'""33.762226"'"","'""venue_lng"'"":"'""-118.19686"'"","'""needs_info"'"":false}";

其他

var flyer2 = {"id":20,"img_url":"http://images.awfbeat.com/48395b02-59e5-4e26-b7bc-8c603008c9c4","img_width":0,"img_height":0,"popularity":0,"color":"#67c547","tag":"Jul 10","category":"festivals","title":"Darth Vader'''u0027s '''"Annihilator'''"","title_slug":"darth-vader-s-annihilator-","performer":"","performer_sort":"xxxx","posted":"2012-03-11 04:09:20.0","facebook_event_id":"","venue_postal":"90802","venue_name":" Aquarium of the Pacific","venue_street":"100 Aquarium Way","venue_city":"Los Angeles","venue_region_abbr":"CA","venue_lat":"33.762226","venue_lng":"-118.19686","needs_info":false};

或者使用单个配额。