Reputation: 2625
I'm new to php and Json and i'm trying to decode the command line Json strings , but the Json decode function gives null while decoding , i'm sending the data in the correct format not sure why it is going wrong .I have checked with the threads and the passing the data as per the url http://php.net/manual/en/function.json-decode.php
.
Here is my code
<? php
if($argc==2)
{
$jsonin=$argv[1];
echo $jsonin."\xA";
$jsonin="'".$jsonin."'";
echo $jsonin ."\xA";
$cmdval=json_decode($jsonin);
if($cmdval)
{
echo $cmdval->{'id'};
}
else
{
echo "Bad string" ;
}
}
else
{
echo "No arguments";
}
?>
This is how i'm passing it to the code
php5 jsonparse.php '{ "time1":"2014/10/30 21:30:00", "time2":"2014/10/31 21:30:00" }'
following are the outputs
{ "time1":"2014/10/30 21:30:00", "time2":"2014/10/31 21:30:00"}
'{ "time1":"2014/10/30 21:30:00", "time2":"2014/10/31 21:30:00"}'
Bad string
Upvotes: 0
Views: 182
Reputation: 819
I have tried this and it works like a charm :
if($argc==2)
{
$jsonin=$argv[1];
$cmdval=json_decode($jsonin);
if($cmdval)
{
echo "You did it right !\r\n";
print_r($cmdval);
}
else
{
echo "Bad string" ;
}
}
else
{
echo "No arguments";
}
And i ran it as below:
php test.php '{ "time1":"2014/10/30 21:30:00", "time2":"2014/10/31 21:30:00" }'
Upvotes: 1
Reputation: 655
Why you are doing this? $jsonin="'".$jsonin."'";
Please remove this code and try again.
Upvotes: 1