How to Json Decode in PHP using Services_JSON package of pear(PHP Catchable fatal error: Object of class stdClass could not be converted to string )

256
September 12, 2017, at 1:18 PM

I want to encode/decode an array using JSON . As i have php 5.1.6 , i am using pear's (http://pear.php.net/pepr/pepr-proposal-show.php?id=198) package. Using it i can encode but , i am unable to decode i tried to read the doc , but didn't understand anything.Here is my code:

<?php  include("/home/gpreeti/php/JSON.php");
$json = new Services_JSON();
$marks = array(
            "mohammad" => array (
               "physics" => 35,
               "maths" => 30,
               "chemistry" => 39
            ),
            "qadir" => array (
               "physics" => 30,
               "maths" => 32,
               "chemistry" => 29
            ),
            "zara" => array (
               "physics" => 31,
               "maths" => 22,
               "chemistry" => 39
            )
         );
$marks=$json->encode($marks);
print"$marks\n";
$marks = $json->decode($marks);
#var_dump($marks);
print"$marks";
?>

On running it , i am getting this

{"mohammad":{"physics":35,"maths":30,"chemistry":39},"qadir":{"physics":30,"maths":32,"chemistry":29},"zara":{"physics":31,"maths":22,"chemistry":39}}
PHP Catchable fatal error:  Object of class stdClass could not be converted to string in /servers/scratch05/gpreeti/php_pgms/test_json.php on line 26

Please Help, Thanks

Answer 1

When you have array/object you need to display it using print_r / var_dump. print is used for string

$marks=$json->encode($marks);
print"$marks\n";
$marks = $json->decode($marks);
#var_dump($marks);
print_r($marks); //change this line

EDIT To get array you need to do following changes:

$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
$marks = $json->decode($marks);
READ ALSO
Calculate CRC16 X25 in php

Calculate CRC16 X25 in php

I need help in calculating CRC X25 variation, i have a Java tool and also an online tool that give the correct conversion, but i haven't been able to reproduce it myself

293
Send custom data via stripe.js

Send custom data via stripe.js

Path Custom data to stripejs

170
Confused about echo &ldquo;{$a[0][&#39;download&#39;]}&rdquo;

Confused about echo “{$a[0]['download']}”

I accidentally tested this today, can anyone explain to me why this works and what it is?

132