JavaScript 序列化相关

如果在客户端,想将信息临时记录在Cookie里面,或者是记录在数据库其他什么地方,应该怎么办?

 

就拿Cookie为例,因为Cookie 的 value 类型一定一个 URI的字符串,那么,很简单,只要是把要保存的数据直接复制给Cookie 就行了,只要注意在复制之前要进行encodeURIComponent(value)操作就行了。

但是如果你按照“对象”的思想来在客户端进行编程,于是你就要将一个对象保存到Cookie里面,怎么办?直接赋值会造成数据的丢失,那么,你要是说还干嘛非要面向对象,粗糙点将就着吧,那么,您还真不用看了。

 

实际上,有点编程经验的人就明白,这时候,我们需要考虑序列化的问题了,于是,我们有了两个方案:

1.JSON 序列化

2.“标准序列化”

 

对于这两个的区别,我们要思考的是反序列化的时候,为什么?因为都是将其序列化为字符串,而反序列化将其从字符串的形式上重新返回到对象形态。

按照 http://www.json.org/json.js

来看,JSON 序列化方式,在反序列化的时候对于复杂的类型,不能很好的转换,我举个例子:

This entry was posted in 未分类. Bookmark the permalink.

6 Responses to JavaScript 序列化相关

  1. 磊哥 says:

    哥哥啊,webservice可以直接返回json的。来个josn.d就可以解析了。

  2. 守護~︶箬 says:

    http://www.cnblogs.com/birdshome/archive/2005/03/25/109158.html
    这个方案可以处理复杂对象的话,要考虑一下…
    能处理的就是那几个case的类型,js中的一些自定义对象都不能进行序列化。

  3. llj099 says:

    @守護~︶箬
    —————————————
    恩,实际上,还是可以的~呵呵

    我试验了一下,通过,你的什么类型,不能用?

  4. Anonymous says:

    有个问题啊,json.js和 jquery.js方在一块好像有脚本错误,不知道是什么原因!
    博客后台就是这样的

  5. 守護~︶箬 says:

    @llj099
    —————————————
    汗。看错,没有看到default下对任意自定义对象的处理..

  6. ying says:

    我靠,这里的人咋都这么专业?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: Commenter is allowed to use '@User+blank' to automatically notify your reply to other commenter. e.g, if ABC is one of commenter of this post, then write '@ABC '(exclude ') will automatically send your comment to ABC. Using '@all ' to notify all previous commenters. Be sure that the value of User should exactly match with commenter's name (case sensitive).

llj098's weblog is Digg proof thanks to caching by WP Super Cache