PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 30, 2022

[FIXED] 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 )

 August 30, 2022     json, pear, php     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing