0

How do I use unserialize method instead of json_decode, by inject vendor/magento/framework/Serialize/Serializer/

 foreach ($results as $result) {
            $incrementId = $result['increment_id'];
            $additionalInformation = $result['additional_information'];
            $additionalInformationArr = json_decode($additionalInformation);
            $addressVerificationJson = $additionalInformationArr->shipping_address_verification;
            $addressVerificationArr = json_decode($addressVerificationJson);
            $postalCode = $addressVerificationArr->postal_code;
}

1 Answer 1

3

You have to inject Magento\Framework\Serialize\SerializerInterface in your constructor

namespace Vendor\Module;

use Magento\Framework\Serialize\SerializerInterface;

class MyClass
{
    /**
     * @var SerializerInterface
     */
    protected $serializer;

    public function __construct(SerializerInterface $serializer)
    {
        $this->serializer = $serializer;
    }
}

Than you can use it in your code

 foreach ($results as $result) {
            $incrementId = $result['increment_id'];
            $additionalInformation = $result['additional_information'];
            $additionalInformationArr = $this->serializer->unserialize($additionalInformation);
            $addressVerificationJson = $additionalInformationArr->shipping_address_verification;
            $addressVerificationArr = $this->serializer->unserialize($addressVerificationJson);
            $postalCode = $addressVerificationArr->postal_code;
}

Hope it helps

3
  • Hi, thanks for the reply, does "decode" is a function for serializer?
    – Paul Fan
    Dec 8, 2020 at 21:10
  • ah sorry, is unserialize instead of decode Dec 8, 2020 at 21:16
  • 1
    thanks! Its work great, just need minor fix
    – Paul Fan
    Dec 8, 2020 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.