简体   繁体   中英

String decode to Json in php

嗨,我有一个api调用,返回如下所示的字符串,我需要将其转换为JSON对象进行处理。

"a:1:{s:19:\"is_featured_service\";b:0;}"

That's a serialize() d string. unserialize() it, then json_encode() it:

<?php
$string = "a:1:{s:19:\"is_featured_service\";b:0;}";
$json = json_encode(unserialize($string));
var_dump($json);

Be careful, though. Per PHP manual:

Warning Do not pass untrusted user input to unserialize() regardless of the options value of allowed_classes. Unserialization can result in code being loaded and executed due to object instantiation and autoloading, and a malicious user may be able to exploit this. Use a safe, standard data interchange format such as JSON (via json_decode() and json_encode()) if you need to pass serialized data to the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM