php JSON 与数据库中的序列化数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1306740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON vs. Serialized Array in database
提问by Inez
What are the advantages and disadvantages of storing JSON data in MySQL database vs. serialized array?
将 JSON 数据存储在 MySQL 数据库中与序列化数组的优缺点是什么?
回答by Mark Tomlin
- JSON encode() & decode()
- PHP Version >= 5.0.0
- Nesting Limit of 20.
- PHP Version >= 5.2.3
- Nesting Limit of 128.
- PHP Version >= 5.3.0
- Nesting Limit of 512.
- Small footprint vs PHP's serialize'd string.
- PHP Version >= 5.0.0
- serialize() & unserialize()
- PHP Version >= 4.0.0
- Methods are not lost on PHP Datatype Object.
- __wakeup() magic method called on any object being unserialize. (VERY POWERFUL)
- It has been noted that it is some times best the base64 encodestrings put into the database, and base64 decodestrings taken out of the database with this function, as there are some issues with the handling of some white space characters.
- PHP Version >= 4.0.0
- JSON编码() &解码()
- PHP 版本 >= 5.0.0
- 嵌套限制为 20。
- PHP 版本 >= 5.2.3
- 嵌套限制为 128。
- PHP 版本 >= 5.3.0
- 嵌套限制为 512。
- 与 PHP 的序列化字符串相比,占用空间小。
- PHP 版本 >= 5.0.0
- 序列化()和反序列化()
The choice is yours.
这是你的选择。
回答by Marius
Pro JSON:
专业JSON:
- The JSON data can be used by many different languages, not just PHP
- JSON data is human readable and writable.
- It takes up less space
- It is faster to encode JSON than to serialize
- JSON 数据可以被许多不同的语言使用,而不仅仅是 PHP
- JSON 数据是人类可读和可写的。
- 它占用更少的空间
- 编码 JSON 比序列化更快
Pro Serialized Array:
专业序列化阵列:
- It is faster do unserialize than to JSON decode
- 反序列化比 JSON 解码更快
As the comments indicate, JSON takes up less space than a serialize array. I also checked whether JSON or Serializing is faster, and surprisingly, it is faster to JSON encode than to Serialize. It is faster to unserialize than to JSON decode though.
正如评论所示,JSON 比序列化数组占用的空间更少。我还检查了 JSON 或 Serializing 是否更快,令人惊讶的是,JSON 编码比 Serialize 更快。反序列化比 JSON 解码更快。
This is the script I used to test:
这是我用来测试的脚本:
<?php
function runTime(){
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
return $mtime;
}
?>
<pre>
<?php
$start = runTime();
$ser;
for($i=0; $i<1000; $i++){
$a = array(a => 1, x => 10);
$ser = serialize($a);
}
$total = runTime() - $start;
echo "Serializing 1000 times took \t$total seconds";
?>
<?php
$start = runTime();
$json;
for($i=0; $i<1000; $i++){
$a = array(a => 1, x => 10);
$json = json_encode($a);
}
$total = runTime() - $start;
echo "JSON encoding 1000 times took \t$total seconds";
?>
<?php
$start = runTime();
$ser;
for($i=0; $i<1000; $i++){
$a = unserialize($ser);
}
$total = runTime() - $start;
echo "Unserializing 1000 times took \t$total seconds";
?>
<?php
$start = runTime();
$json;
for($i=0; $i<1000; $i++){
$a = json_decode($json);
}
$total = runTime() - $start;
echo "JSON decoding 1000 times took \t$total seconds";
?>
</pre>
回答by Alan Storm
Portability: Winner JSON. JSON is supported on a wider variety of platforms, while PHP de-serialization is only supported (as far as I know) by PHP. While it's possible to parse either format in any language, JSON has more pre-built libraries.
可移植性:赢家 JSON。JSON 在更广泛的平台上受支持,而 PHP 反序列化仅受 PHP 支持(据我所知)。虽然可以用任何语言解析任何一种格式,但 JSON 有更多的预构建库。
Future Proof: Winner JSON. JSON is a "standard", in the sense that Javascript is a standard, and isn't likely to change anytime in the future. The PHP group has made no promises about the future of the serialization format, and while it's unlikely to change in the future, the fact that a single group controls the format means you may end up with future data that's unreadable.
未来证明:赢家 JSON。JSON 是一种“标准”,因为 Javascript 是一种标准,并且在未来的任何时候都不太可能改变。PHP 组没有对序列化格式的未来做出任何承诺,虽然将来不太可能改变,但单个组控制格式的事实意味着您可能最终会得到不可读的未来数据。
Fidelity: Winner PHP. PHP serialization will allow you to store data with native PHP data types, including objects defined by custom classes. JSON will only allow you to store generic primitive types, lists of primitive types ("arrays") and key/value pair Objects. PHP Serialization may provide some advantages here if you're developing a PHP application.
富达:赢家 PHP。PHP 序列化将允许您使用本机 PHP 数据类型存储数据,包括由自定义类定义的对象。JSON 只允许您存储通用原始类型、原始类型列表(“数组”)和键/值对对象。如果您正在开发 PHP 应用程序,PHP 序列化可能会在此处提供一些优势。
File Size: JSON has a slight win here, as PHP's current serialization format is more verbose (as it's storing more information).
文件大小:JSON 在这里略胜一筹,因为 PHP 当前的序列化格式更加冗长(因为它存储了更多信息)。
Performance: Who knows, it depends, profile.
性能:谁知道,这取决于配置文件。
Conclusion: Go with JSON unless you have a compelling reason to use PHP Serialization .
结论:除非您有令人信服的理由使用 PHP 序列化,否则请使用 JSON。
回答by mere-teresa
Are you using your datas only with PHP ? If yes : arrays, if no : JSON.
您是否仅在 PHP 中使用您的数据?如果是:数组,如果不是:JSON。
Pro Array
专业阵列
- sessions used serialization : I think it's faster than json_encode/decode (not quite sure)
- many functions on arrays in PHP (sorting/merging/...)
- 会话使用序列化:我认为它比 json_encode/decode 快(不太确定)
- PHP 中数组的许多函数(排序/合并/...)
Pro JSON
专业 JSON
- JSON is know in other languages and web languages
- less verbose in database
- many tools, like XML : JSON SChema
- JSON 在其他语言和网络语言中广为人知
- 数据库中不那么冗长
- 许多工具,如 XML : JSON SCHema
回答by Tom Haigh
JSON is more portable, i.e. you can more easily read/write to it from different languages etc. If you used PHP serialized arrays you would only be able to easily use PHP to access it.
JSON 更便携,即您可以更轻松地从不同语言等读取/写入它。如果您使用 PHP 序列化数组,您将只能轻松使用 PHP 来访问它。
回答by Thinker
There was a lot of such questions on SO.
在 SO 上有很多这样的问题。
Preferred method to store PHP arrays (json_encode vs serialize)
存储 PHP 数组的首选方法(json_encode 与序列化)
In short: JSON is better for simple data, but it doesn't distinguish difference between object and associative array. Serialized data are bigger.
简而言之:JSON 更适合简单数据,但它不区分对象和关联数组之间的区别。序列化数据更大。
回答by laurens
Use json for for arrays and communication with Javascript or other language. Use serialize for object or any internal PHP work for the current running script.
将 json 用于数组和与 Javascript 或其他语言的通信。将序列化用于对象或当前运行脚本的任何内部 PHP 工作。
回答by tim
if youre trying to get around quotes and special characters in your JSON.stringify(obj) ,you can do so in PHP using it's database-specific escaping methods.
如果你试图绕过 JSON.stringify(obj) 中的引号和特殊字符,你可以在 PHP 中使用它的特定于数据库的转义方法来做到这一点。
<?php
mysql_real_escape_string(htmlspecialchars($value))
?>
you can now store this safely and decode it when you read it back out
您现在可以安全地存储它并在您读回时对其进行解码
回答by Francois Bourgeois
JSON beats serialization as most answers already pointed out. I think the biggest advantage is its platform independency. You might have other applications communicate with you database and they might not have anything to do with php.
正如大多数答案已经指出的那样,JSON 优于序列化。我认为最大的优势是它的平台独立性。您可能有其他应用程序与您的数据库通信,它们可能与 php 无关。
But bothsolutions violate database normalization. You database will not even be in first normal formso you cannot take advantage of any database feature like, say, searching. A better approach is to use object relational mapping. There are good libraries out there - consider for example doctrine ORM.
但是这两种解决方案都违反了数据库规范化。您的数据库甚至不会是第一范式,因此您无法利用任何数据库功能,例如搜索。更好的方法是使用对象关系映射。那里有很好的库——例如,考虑到Dotty ORM。
回答by SequenceDigitale.com
I just had this big problem with php serialize. I stored a lot of data in a single field in which i used unserializeto read.
我刚刚在php serialize 上遇到了这个大问题。我在一个字段中存储了大量数据,我使用反序列化来读取。
What happened is that I got some corrupt data in that field. Serialize map the data with codes like 'a','s' and 'N'. If there is corrupt data, the map failed. It will show a php error that the unserialize function is unable to work because of byte code error.
发生的事情是我在该领域得到了一些损坏的数据。使用“a”、“s”和“N”等代码序列化映射数据。如果存在损坏的数据,则映射失败。它会显示一个php错误,因为字节码错误,unserialize函数无法工作。
So my point is to avoid serialize. Go with JSON, way safer and you won't bang your head on future majors problems.
所以我的观点是避免serialize。使用JSON,这样更安全,你不会在未来的专业问题上大惊小怪。
For me, no more serialize.
对我来说,不再有serialize。

