Getting JSON data in PHP

Posted by domagoj412 on Stack Overflow See other posts from Stack Overflow or by domagoj412
Published on 2010-03-24T21:08:37Z Indexed on 2010/03/24 21:13 UTC
Read the original article Hit count: 301

Filed under:
|
|
|

I have a JSON data like this:

{
 "hello":
  {
   "first":"firstvalue",
   "second":"secondvalue"
  },
  "hello2":
  {
   "first2":"firstvalue2",
   "second2":"secondvalue2"
  }
}

I know how to retrieve data from object "first" (firstvalue) and second (secondvalue) but I would like to loop trough this object and as a result get values: "hello" and "hello2"...

This is my PHP code:

<?php 

$jsonData='{"hello":{"first":"firstvalue","second":"secondvalue"},""hello2":{"first2":"firstvalue2","second2":"secondvalue2"}}';
$jsonData=stripslashes($jsonData);
$obj = json_decode($jsonData);

echo $obj->{"hello"}->{"first"}; //result: "firstvalue"
?>

Can it be done?

© Stack Overflow or respective owner

Related posts about php

Related posts about JSON