php和js序列化,PHP中serialize和json序列化与反序列化的区别

在PHP中,serialize和json两种方式对一个对象或数组进行序列化或反序列化有什么区别呢?

假设一个对象和一个数组:

PHP

1

2

3

4$web=newstdClass;

$web->site='tantengvip';

$web->owner='tuntun';

$web->age=5;

PHP

1

2

3

4$web=array();

$web['site']='tantengvip';

$web['owner']='tuntun';

$web['age']=5;

对它们分别用serialize函数和unserialize函数进行序列化和反序列化,看看打印结果分别是什么,如下:

使用serialize方式:

PHP

1

2

3

4var_dump(serialize($web));

var_dump(unserialize(serialize($web)));

var_dump(json_encode($web));

var_dump(json_decode(json_encode($web)));

结果:

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13string'O:8:"stdClass":3:{s:4:"site";s:10:"tantengvip";s:5:"owner";s:6:"tuntun";s:3:"age";i:5;}'(length=87)

object(stdClass)[127]

public'site'=>string'tantengvip'(length=10)

public'owner'=>string'tuntun'(length=6)

public'age'=>int5

string'{"site":"tantengvip","owner":"tuntun","age":5}'(length=46)

object(stdClass)[127]

public'site'=>string'tantengvip'(length=10)

public'owner'=>string'tuntun'(length=6)

public'age'=>int5

使用json方式:

PHP

1

2

3

4var_dump(serialize($web));

var_dump(unserialize(serialize($web)));

var_dump(json_encode($web));

var_dump(json_decode(json_encode($web),true));

结果

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13string'a:3:{s:4:"site";s:10:"tantengvip";s:5:"owner";s:6:"tuntun";s:3:"age";i:5;}'(length=74)

array(size=3)

'site'=>string'tantengvip'(length=10)

'owner'=>string'tuntun'(length=6)

'age'=>int5

string'{"site":"tantengvip","owner":"tuntun","age":5}'(length=46)

array(size=3)

'site'=>string'tantengvip'(length=10)

'owner'=>string'tuntun'(length=6)

'age'=>int5

我们发现,对于前面定义的这样一个对象或数组,用serialize和json进行序列化,反序列化回来的结果和原来是一样的,并没有什么区别,除了序列化的格式不同而已。

那么它们到底有何区别?以下文字总结很好,就不自己加以说明了,可以写代码验证。(链接)

使用json序列化和反序列化

优势:变量序列化后依然可读

可以给其他系统使用,因为JSON格式是标准的

劣势:只对UFT-8的数据有效,其他编码可能不能很好工作

只对stdClass类的示例有效

使用serialize方式序列化和反序列化

优势:允许非UTF-8的变量

支持除了stdClass 示例外的其他实例

劣势:编码后的文本对人来说是不可读的

无法被其他语言的系统引用

好,写个代码看看:

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18classTest

{

private$pri='pri';

public$class='Test';

publicfunction__construct()

{

$this->class='Test construct';

$this->pri='pri construct';

}

}

$test=newTest();

var_dump(serialize($test));

var_dump(unserialize(serialize($test)));

var_dump(json_encode($test));

var_dump(json_decode(json_encode($test)));

结果:

PHP

1

2

3

4

5

6

7

8

9

10string'O:4:"Test":2:{s:9:"Testpri";s:13:"pri construct";s:5:"class";s:14:"Test construct";}'(length=86)

object(Test)[127]

private'pri'=>string'pri construct'(length=13)

public'class'=>string'Test construct'(length=14)

string'{"class":"Test construct"}'(length=26)

object(stdClass)[127]

public'class'=>string'Test construct'(length=14)

我们发现,json序列化和反序列化丢失了类中的私有成员变量,而serialize序列化和反序列化只要是类的变量都可以,但是类的成员方法都无法进行序列化和反序列化。

在一般情况,还是使用json比较好,因为json是跨平台的通用格式,除了json,用xml也比较好。那在什么时候使用serialize方式呢?

在对一个类进行serialize反序列化的时候会默认调用魔术方法__wakeUp(),这样就使得对象能够重新建立起序列化时未能保留的各种状态。例如:数据库连接等。那就是另外一个问题了,这里不做深究了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/411960.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[导入]ASP.NET 2.0中Page事件的执行顺序

文章来源:http://blog.csdn.net/21aspnet/archive/2007/03/20/1535517.aspx 转载于:https://www.cnblogs.com/zhaoxiaoyang2/archive/2007/03/21/816354.html

[Swift]LeetCode859. 亲密字符串 | Buddy Strings

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址&a…

三瞬属性matlab,matlab:out of memory 1

问题三:Increase the size of the swap file.wap space的设置与使用的操作系统有关,具体的如下:1.UNIXInformation about swap space can be procured by typing pstat -s at the UNIX command prompt. For detailed information on changing…

[导入]C#中TextBox只能输入数字的代码

文章来源:http://blog.csdn.net/21aspnet/archive/2007/03/20/1535640.aspx 转载于:https://www.cnblogs.com/zhaoxiaoyang2/archive/2007/03/21/816242.html

实验1c语言开发环境使用和数据类型、运算符和表达式

实验结论 由于这一次是第一次做实验有很多东西不熟悉 比如忘记加分号&#xff0c;用中文输入法打不对符号等等。总之经过这实验我学到了很多。#include <stdio.h> int main() {int x;printf("输入一个整数: \n");scanf("%d",&x);// 在处填写相应…

[vue] vue怎么改变插入模板的分隔符?

[vue] vue怎么改变插入模板的分隔符&#xff1f; optionMergeStrategies类型&#xff1a;{ [key: string]: Function }默认值&#xff1a;{}用法&#xff1a;Vue.config.optionMergeStrategies._my_option function (parent, child, vm) {return child 1}const Profile Vue…

php地址转换成经纬度,百度地图 获取地址转换为经纬度

html>根据地址查询经纬度a.{margin-right:100px;}style"position: absolute;margin-top:30px;width: 730px;height: 590px;top: 50px;border: 1px solid gray;overflow:hidden;">var map new BMap.Map("container");var point new BMap.Point(113.…

Nhibernate学习起步之many-to-one篇(转)

1. 学习目的: 通过进一步学习nhibernate基础知识&#xff0c;在实现单表CRUD的基础上&#xff0c;实现两表之间one-to-many的关系. 2. 开发环境必要准备 开发环境: windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition 必要准备: 学习上篇文章单…

[vue] 你了解什么是函数式组件吗?

[vue] 你了解什么是函数式组件吗&#xff1f; 函数式组件&#xff1a;需要提供一个render方法&#xff0c; 接受一个参数&#xff08;createElement函数&#xff09;&#xff0c; 方法内根据业务逻辑&#xff0c;通过createElement创建vnodes&#xff0c;最后return vnodescre…

列表元素的几种统计方法总结(嵌套列表)

&#xff08;1&#xff09;列表中的count方法(速度慢) #嵌套列表类型的统计 l [[1,2,3,4,5],[1,2,3,4,5],[5,6,7,8,9]] dictionary {} s set(l) for i in s:dict[i] l.count(i)&#xff08;2&#xff09;字典&#xff08;速度慢&#xff09; l [[1,2,3,4,5],[1,2,3,4,5],[5…

SQL Server数据库优化方案

SQL Server数据库优化方案 查询速度慢的原因很多&#xff0c;常见如下几种&#xff1a;1、没有索引或者没有用到索引(这是查询慢最常见的问题&#xff0c;是程序设计的缺陷)2、I/O吞吐量小&#xff0c;形成了瓶颈效应。3、没有创建计算列导致查询不优化。4、内存不足5、网络速度…

[vue] vue的:class和:style有几种表示方式?

[vue] vue的:class和:style有几种表示方式&#xff1f; :class 绑定变量 绑定对象 绑定一个数组 绑定三元表达式 :style 绑定变量 绑定对象 绑定函数返回值 绑定三元表达式个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷…

关于如何清除某个特定网站的缓存---基于Chrome浏览器

1、清除浏览器缓存 直接在浏览器设置里面清除浏览器的缓存会清除所有网站的缓存信息&#xff0c;这在某些时候是非常不方便的&#xff0c;毕竟不只有测试网站&#xff0c;还会有一些我们不想清除的信息也会被清除掉&#xff1b; 2、通过F12功能去清除浏览器缓存 转载于:https:/…

php中for循环流程图,PHP for循环

PHP for循环可以用来遍历一组指定的次数的代码。如果迭代次数已知&#xff0c;则应优先考虑使用for循环&#xff0c;否则使用while循环。for循环的语法for(initialization; condition; increment/decrement){ //code to be executed }for循环流程图示例代码-<?php for($n1;…

山西DotNet俱乐部网站改版成功

山西DotNet俱乐部改版成功网址为:http://www.dotnet.sx.cn或http://www.xy8.cn欢迎大家光临! 转载于:https://www.cnblogs.com/axzxs2001/archive/2007/04/05/700983.html

[vue] vue的is这个特性你有用过吗?主要用在哪些方面?

[vue] vue的is这个特性你有用过吗&#xff1f;主要用在哪些方面&#xff1f; vue中is的属性引入是为了解决dom结构中对放入html的元素有限制的问题<ul><li ismy-component></li> </ul>个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放…

Spring中AOP切面编程学习笔记

注解方式实现aop我们主要分为如下几个步骤&#xff1a;  1.在切面类&#xff08;为切点服务的类&#xff09;前用Aspect注释修饰&#xff0c;声明为一个切面类。  2.用Pointcut注释声明一个切点&#xff0c;目的是为了告诉切面&#xff0c;谁是它的服务对象。&#xff08;此…

Good Web

Good Web http://www.jxue.com/job/resume/ Englishhttp://www.jxue.com/zt/06zt/resume/http://www.cnrencai.com/ Jobposted on 2007-04-10 00:18 Steveson 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblogs.com/Steveson/archive/2007/04/10/706450.h…

asp.net 生命周期中的时间流程

一、初始化 当页面被提交请求第一个方法永远是构造函数。您可以在构造函数里面初始一些自定义属性或对象&#xff0c;不过这时候因为页面还没有被完全初始化所以多少会有些限制。特别地&#xff0c;您需要使用HttpContext对象。当前可以使用的对象包括QueryString, Form以及Coo…

[vue] 怎么配置使vue2.0+支持TypeScript写法?

[vue] 怎么配置使vue2.0支持TypeScript写法&#xff1f; 配置ts-loader&#xff0c;tsconfig增加类型扩展&#xff0c;让ts识别vue文件vue文件中script里面换成ts写法&#xff0c; 需要增加几个ts扩展的package&#xff0c; 比如vue-property-decorator个人简介 我是歌谣&…