Issue
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
Solution
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);
Answered By - B. Desai Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.