Reputation: 289
consider the following url
index.php?json={uid:guest|10001441}
I am not able to decode the data .
I need the output as {"uid":"guest|10001441"}
.
The code i m using is
if (isset($_GET['json']))
{
$url_data = $_GET['json'];
$decoded_data = json_decode($url_data,true);
var_dump($decoded_data);
}
But it gives me output as NULL
. What am i doing wrong?? Do i need to pass data in a different format??
Upvotes: 0
Views: 1190
Reputation: 32790
If you pass it like this it will work: ?json={"uid":"guest|10001441"}
But I am not sure whether it is a proper method.
Upvotes: 1
Reputation: 56
It seems impossible to decode a JSON string without double quotes, as it's hard to determine whether a JSON string "{A:B:C}"
is encoded from {"A":"B:C"}
or from {"A:B":"C"}
You can use urlencode() to encode the JSON string or just ignore the extreme cases and add the double quotes manually to make the JSON string valid, hope this help :D
Upvotes: 2
Reputation: 4142
you can encode url parameter with base64_encode()
$json=base64_encode('{"uid":"guest|10001441"}');
url will look like this
index.php?json=eyJ1aWQiOiJndWVzdHwxMDAwMTQ0MSJ9
then do like this :-
if (isset($_GET['json']))
{
$url_data = base64_decode($_GET['json']);
$decoded_data = json_decode($url_data,true);
var_dump($decoded_data);
}
Upvotes: 0
Reputation: 1913
The JSON you have in your URL is not valid. PHP's json_decode looks for double quotes around the uid
and it's value.
EDIT: Rather than storing the data in the URL as json could you not store the data as key value pairs such as
?uid=value
And then you could do something like
$data = json_encode( $_GET );
Upvotes: 0
Reputation: 76636
The data you're passing is not valid JSON. It doesn't have double quotes around it currently. Try this:
if (isset($_GET['json']))
{
$url_data = '"'.$_GET['json'].'"';
$decoded_data = json_decode($url_data,true);
var_dump($decoded_data);
}
Output:
string(20) "{uid:guest|10001441}"
Also, I'd suggest using POST
instead of GET
here.
Upvotes: 1