序列化为javascript对象

时间:2014-11-16 07:01:31

标签: java javascript spring-mvc serialization

我正在开发一个Spring MVC,我想在html输出中插入javascript以进行分析。我对序列化只有部分熟悉,但我认为它可以很好地完成工作,而不是手动构建包含javascript的字符串。

是否可以生成以下代码段?任何指针都会很棒!

"emd"   :   new   Date('6/6/2014')

更新

我需要输出一个javascript对象,它有许多可能很复杂的字段。因此,在后端我将所有数据收集到包含所有信息的java bean中,并且我计划使用Jackson映射器转换为我可以通过JSP输出的字符串。

生成上面的代码片段似乎并不简单,不确定它是否可行。对于上下文,其余的javascript看起来像这样。

 Analytics.items["item_123"] = {

     //ratings and reviews

    "rat"   : a.b,   //the decimal value for the rating

    "rev"   : xxxx,   //integer

    //list of flags that indicate how the product was displayed to the customer

    //add as needed...tracking code will pick up flags as needed when they are available

    "dec"   : ["mbe",   "green",   "recycled"], 


    //delivery messaging

    "delivery"   : {

            "cd"   :  new   Date()   //current date

            "offers"   : [{

                            "type"   :   "abcd" 

                            "emd"   :   new   Date('6/6/2014'),   

                            "weekend"   :   true

                    }

            ]

    },

};

1 个答案:

答案 0 :(得分:0)

JSON.stringify应该做到这一点。它将内置到您的浏览器中,除非您使用的是非常旧浏览器,在这种情况下,您可以使用polyfill

相关问题