我想使用JSON编码/解码数组。由于我拥有Php 5.1.6,因此我使用的是Pear's(http://pear.php.net/pepr/pepr-proposal-show.php?id=198)。使用它,我可以编码,但是,我无法解码我尝试阅读文档,但什么都不了解。我的代码是:
<?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"$marksn";
$marks = $json->decode($marks);
#var_dump($marks);
print"$marks";
?>
在运行它时,我得到了这个
{"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
请帮忙,谢谢
当您具有数组/对象时,您需要使用print_r / var_dump
显示它。打印用于字符串
$marks=$json->encode($marks);
print"$marksn";
$marks = $json->decode($marks);
#var_dump($marks);
print_r($marks); //change this line
edit 要获得以下更改需要进行的数组:
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
$marks = $json->decode($marks);