0

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

2 Answers 2

1

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" }'
Sign up to request clarification or add additional context in comments.

Comments

1

Why you are doing this? $jsonin="'".$jsonin."'";
Please remove this code and try again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.