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 )
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 )
我想 encode/decode 使用 JSON 的数组。因为我有 php 5.1.6 ,所以我正在使用 pear 的 (http://pear.php.net/pepr/pepr-proposal-show.php?id=198) 包。
使用它我可以编码但是,我无法解码我试图阅读文档,但不明白 anything.Here 是我的代码:
<?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";
?>
在 运行 上,我得到了这个
{"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
请帮助,
谢谢
当您有 array/object 时,您需要使用 print_r / var_dump
显示它。打印用于字符串
$marks=$json->encode($marks);
print"$marks\n";
$marks = $json->decode($marks);
#var_dump($marks);
print_r($marks); //change this line
编辑 要获取数组,您需要进行以下更改:
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
$marks = $json->decode($marks);
我想 encode/decode 使用 JSON 的数组。因为我有 php 5.1.6 ,所以我正在使用 pear 的 (http://pear.php.net/pepr/pepr-proposal-show.php?id=198) 包。 使用它我可以编码但是,我无法解码我试图阅读文档,但不明白 anything.Here 是我的代码:
<?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";
?>
在 运行 上,我得到了这个
{"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
请帮助, 谢谢
当您有 array/object 时,您需要使用 print_r / var_dump
显示它。打印用于字符串
$marks=$json->encode($marks);
print"$marks\n";
$marks = $json->decode($marks);
#var_dump($marks);
print_r($marks); //change this line
编辑 要获取数组,您需要进行以下更改:
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
$marks = $json->decode($marks);