Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to decode a JSON string into an object in PHP

3 Answers

0 votes
$json_string = '{"name": "Tim", "age": 42, "profession": "Programmer"}';
$object = json_decode($json_string);

printf('%s %d %s', $object->name, $object->age, $object->profession);


/*
run: 
 
Tim 42 Programmer
 
*/

 



answered Sep 3, 2017 by avibootz
edited Sep 3, 2017 by avibootz
0 votes
$json_string = '{"name": "Tim", "profession": "Programmer", "languages": ["PHP", "JAVA"]}';
$object = json_decode($json_string);
 
printf('%s %s %s %s', $object->name, $object->profession, 
                      $object->languages[0], $object->languages[1]);


/*
run: 
 
Tim Programmer PHP JAVA
 
*/

 



answered Sep 3, 2017 by avibootz
0 votes
$json_string = '{"name": "Tim", "profession": "Programmer", "languages": ["PHP", "JAVA"]}';
$object = json_decode($json_string);
                      
echo "<pre>";
var_dump($object);
echo "</pre>";

/*
run: 
 
object(stdClass)#1 (3) {
  ["name"]=>
  string(3) "Tim"
  ["profession"]=>
  string(10) "Programmer"
  ["languages"]=>
  array(2) {
    [0]=>
    string(3) "PHP"
    [1]=>
    string(4) "JAVA"
  }
}
 
*/

 



answered Sep 3, 2017 by avibootz

Related questions

5 answers 377 views
1 answer 237 views
2 answers 248 views
248 views asked Jul 2, 2016 by avibootz
1 answer 143 views
143 views asked Feb 16, 2021 by avibootz
1 answer 224 views
1 answer 167 views
167 views asked Feb 16, 2021 by avibootz
1 answer 143 views
143 views asked Jan 29, 2022 by avibootz
...