dtyrxmoj20617 2014-01-26 05:24
浏览 238

使用Json_encode数据或序列化数据并存储在数据库中的效率更高?

Just came across serialize() in php. Many databases store the data in serialized form so curious to know what is more usefun json or serialize and why?

 Json Data->["Math","Language","Science","History"]
 serialized Data->a:4:{i:0;s:4:"Math";i:1;s:8:"Language";i:2;s:7:"Science";i:3;s:7:"History";}
  • 写回答

2条回答 默认 最新

  • doujiaoang69440 2014-01-26 06:04
    关注

    Serialize() is a native PHP method of storing data in text strings. It can even store objects. And it has a special notation for objects. JSON, it its turn, is a JavaScript's Object Notation. Basically, same thing. But they are used in different domains.

    JSON is usually used for online communication - sending data to/from AJAX scripts, numerous API's, etc. It appeared to be much lighter and readable than XML, thus it competes with it for the most commonly used data transfer standard for APIs. It is also widely used in config files, say, for Composter - where readability is one of the top requirements. Visual readability and easy structure makes JSON the best way to communicate between different platforms and programming languages. This, last phrase is important.

    PHP Serializing is used in cases where you save data in PHP and you restore data back in PHP as well. Say, you can save arrays, or $user object into a PHP session, and then restore them back. Or store a multi-dimensional array in a MySQL field, and then restore it exactly as it was in an $array variable. In most cases Serialized data is not much shorter/lighter than JSON and doesn't usually take less space - it makes it lose readability though. But there are quite powerful tricks with it PHP has - it preserves data types, classes names, visibility options, etc. Also refer to __sleep() and __wakeup() magic class methods allowing you to save objects on the fly and create special constructors to revive them back, again, on the fly.

    In your particular case, if you store data in MySQL database with PHP, and then read it back to PHP - then PHP serialization would be the best way to use. Unless you want to access this data with other languages, or edit manually/visually.

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站