sman
sman

Reputation: 3

How to deserialize data in PHP which is serialized by GSON

I am working on android app with api developed in PHP. I am trying to send an object to api, so I am serializing it with Gson library, and trying to deserialize in PHP code using Unserialize() function. But, its giving error as (!)Notice: unserialize(): Error at offset 0 of 468 bytes in C:\wamp64\www\digiclass\admin\api\upload_pending_results.php on line 5 and finally giving json response as {"error":false,"data":false} Here is my PHP code:

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

currently, I am not performing any operation on data, rather only want to see if the data is deserializing correctly. I have tried to see the serialized object, it is converted as below.

%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

I am not sure if the conversion is going right or what the error might be. Can anyone help?

Upvotes: 0

Views: 93

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57131

The value you are getting is a URL encode JSON string, so first decode it...

$serObject = urldecode($serObject);

and then decode it as JSON...

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

Upvotes: 1

Related Questions