7 Replies - 500 Views - Last Post: 13 October 2012 - 09:59 PM Rate Topic: -----

#1 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Accessing JSON nodes

Posted 13 October 2012 - 10:04 AM

I'm getting the following JSOn response from a web service.

Array
(
    [upload] => Array
        (
            [image] => Array
                (
                    [name] => 
                    [title] => 
                    [caption] => 
                    [hash] => ACkg3
                    [deletehash] => IozBR9NjaVOrMQ2
                    [datetime] => 2012-10-13 17:01:53
                    [type] => image/png
                    [animated] => false
                    [width] => 324
                    [height] => 239
                    [size] => 29844
                    [views] => 0
                    [bandwidth] => 0
                )

            [links] => Array
                (
                    [original] => http://i.imgur.com/ACkg3.png
                    [imgur_page] => http://imgur.com/ACkg3
                    [delete_page] => http://imgur.com/delete/IozBR9NjaVOrMQ2
                    [small_square] => http://i.imgur.com/ACkg3s.jpg
                    [large_thumbnail] => http://i.imgur.com/ACkg3l.jpg
                )

        )

)



I want to retrieve only the node named [original] (line no. 24). How can I access it in PHP?

This post has been edited by nK0de: 13 October 2012 - 10:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Accessing JSON nodes

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Accessing JSON nodes

Posted 13 October 2012 - 10:30 AM

Use the json_decode function to convert it to an object or array. Then just access the element you want like you'd normally do with an object or array.
Was This Post Helpful? 1
  • +
  • -

#3 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Accessing JSON nodes

Posted 13 October 2012 - 10:46 AM

Got it! Thanks Atli :)
Was This Post Helpful? 0
  • +
  • -

#4 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Accessing JSON nodes

Posted 13 October 2012 - 02:07 PM

On a related note, if your JSON object is embedded with Javascript objects, when they are brought over to server-side, you need to convert the Javascript objects to arrays themselves. Sounds bizarre? Here's the premise:
// javascript
var myObject = {
  'key1': 1,
  'key2': 2
};
var myArray = ['foo', myObject];
var myJsonObject = $.toJSON(myArray);
// ... lets assume this now gets passed to client side ...


// php
$myJson = json_decode($_GET['myJsonObject']);
var_export($myJson);


... returns something like this ...
array (
  0 => 'foo',
  1 =>
  stdClass::__set_state(array(
     'key1' => 1,
     'key2' => 2,
  )),
)


... which will cause PHP to have a fit if you try to access it like an array (e.g. $myJson[1][key1]), as it will insist that it is an object. We can get around this by using a custom function that maps an entire JSON object to an array structure, regardless of whether the nodes are array-type or object-type:
function ObjectToArray($d)
{
	$returnValue = $d;

	if (is_object($d)) {
		$returnValue = get_object_vars($d);
	}

	if (is_array($d)) {
		$returnValue = array_map(__FUNCTION__, $d);
	}

	return $returnValue;
}


This function will recurse over the entire JSON object, down to the deepest nodes. Just call it like this:
$myJsonAsArray = ObjectToArray(json_decode($_GET['myJsonObject']));


EDIT: I'm pretty sure this works when Javascript objects are nested in Javascript objects, but if it doesn't, I'd change line 9 to if (is_array($returnValue)) {.

This post has been edited by e_i_pi: 13 October 2012 - 02:19 PM

Was This Post Helpful? 2
  • +
  • -

#5 MrXtremeM  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 13-October 12

Re: Accessing JSON nodes

Posted 13 October 2012 - 02:44 PM

I don't think he needs to worry about objects in the problem he is facing :)
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Accessing JSON nodes

Posted 13 October 2012 - 03:09 PM

View Poste_i_pi, on 13 October 2012 - 09:07 PM, said:

On a related note, if your JSON object is embedded with Javascript objects, when they are brought over to server-side, you need to convert the Javascript objects to arrays themselves.

Setting the second parameter to the json_decode function to true should take care of that for you.
Was This Post Helpful? 3
  • +
  • -

#7 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Accessing JSON nodes

Posted 13 October 2012 - 03:31 PM

@Atli: Hehe I should read the manual more.
Was This Post Helpful? 0
  • +
  • -

#8 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Accessing JSON nodes

Posted 13 October 2012 - 09:59 PM

Atli, I've already set it to true. :) Thanks e_i_pi
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1