如何在PHP中反序列化由GSON序列化的数据

s5a0g9ez  于 2022-11-06  发布在  PHP
关注(0)|答案(1)|浏览(201)

我正在使用PHP开发的api开发Android应用程序。我试图将一个对象发送到api,所以我用Gson库将其序列化,并尝试使用Unserialize()函数在PHP代码中反序列化。但是,它给出了(!)Notice: unserialize(): Error at offset 0 of 468 bytes in C:\wamp64\www\digiclass\admin\api\upload_pending_results.php on line 5错误,最后给出了{"error":false,"data":false} json响应。以下是我的PHP代码:

<?php
//an array to display response
$response = array();
$serObject = $_POST['serObject'];
$response['error'] = false;
$response['data'] = unserialize($serObject);
echo json_encode($response);
?>

目前,我没有在数据上执行任何操作,而只是想看看数据是否正确地反序列化。2我已经尝试查看序列化的对象,它被转换为如下所示。

%7B%22accessToken%22%3A%22b2c1f3d5e8218cfa81d23c4b9b7d6cbc20f82c67ca4b9d92be9bb7680031360a9d95bf1e3ecf42678f1c8b87a4eb5622%22%2C%22clas%22%3A%229%22%2C%22dbname%22%3A%22a_new%22%2C%22records%22%3A%5B%7B%22clas%22%3A%229%22%2C%22id%22%3A1%2C%22marks%22%3A%222%2F8%22%2C%22rollno%22%3A%2212%22%2C%22subject%22%3A%22Maths%22%2C%22temp%22%3A1%7D%2C%7B%22clas%22%3A%229%22%2C%22id%22%3A1%2C%22marks%22%3A%227%2F8%22%2C%22rollno%22%3A%2212%22%2C%22subject%22%3A%22Maths%22%2C%22temp%22%3A2%7D%2C%7B%22clas%22%3A%229%22%2C%22id%22%3A1%2C%22marks%22%3A%223%2F8%22%2C%22rollno%22%3A%2212%22%2C%22subject%22%3A%22Maths%22%2C%22temp%22%3A3%7D%2C%7B%22clas%22%3A%229%22%2C%22id%22%3A1%2C%22marks%22%3A%228%2F8%22%2C%22rollno%22%3A%2212%22%2C%22subject%22%3A%22Maths%22%2C%22temp%22%3A4%7D%5D%2C%22rollno%22%3A%2212%22%7D

我不确定转换是否正确,也不知道可能出现了什么错误。有人能帮忙吗?

mbskvtky

mbskvtky1#

您获得的值是URL编码JSON字符串,因此首先对其进行解码...

$serObject = urldecode($serObject);

然后将其解码为JSON...

$response['data'] = json_decode($serObject, true);

相关问题