Redis Save Array (Json_encode unsuccessful, Serialize succeeded)

Source: Internet
Author: User
To be stored in the form of a Redis array, string type.
Use PHP's own json_encode and Json_decode to convert to JSON (failed).
PHP comes with a serialization function serialize and Unserialize function (success).
What are the specific differences? Is there a hero who speaks of the principles of success and failure?

Reply content:

To be stored in the form of a Redis array, string type.
Use PHP's own json_encode and Json_decode to convert to JSON (failed).
PHP comes with a serialization function serialize and Unserialize function (success).
What are the specific differences? Is there a hero who speaks of the principles of success and failure?

It doesn't seem like a big relationship with Redis. The JSON string for set and get should be the same.

You can just json_encode print out the direct results of the string and json_decode look at it.

I guess it might be json_decode($str, true) less. The argument is written json_decode($str) , resulting in an object instead of an array.

Serialization and JSON are not essentially the same thing, JSON is a data format

Serialization refers to the conversion of data objects in a running environment into streaming data that can be saved to a file or network for use elsewhere.
This stream data is deserialized and reverts to the data object in that language
Each language has its own serialization method

PHP has a more efficient approach than serialize, which is
Igbinary_serialize () and
Igbinary_unserialize ()
These two methods require the installation of the Ibginary module
Configuring When using Redis

$reids->setOption(Redis::OPT_SERIALIZER,Redis::SERIALIZER_IGBINARY);

Redis is automatically serialized, not set after each serialization, and deserialized after each get

For serialization, take PHP

class Person{    private $_name = "default";    public function get_name(){        return $this->_name;    }}$p = new Person;$sp = serialize($p);send2serverB($p);

You serialized an object instance on the a server and you want to use that instance in service B
Of course, the declaration of the class person is also required in Server B to deserialize properly

function recieve_handle($sp){   $p = unserialize($p);   // 反序列后, 能还原Person实例, 能使用实例的方法   // json只是种保存元数据的格式, 无法保存对象   $name = $p->get_name();}

Because the data is to be saved or transmitted (so-called I/O), it is only serialized.
JSON is a commonly used data flow format, but it can only store metadata, unable to express complex object properties and methods

Note that the data must be UTF8,
GBK is not encode.

Is that Json_encode cannot sequence a conversation object

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.