[thelist] json_decode

Renoir B. renoirb at gmail.com
Fri Jun 21 18:28:10 CDT 2013


Hello Bob

If your data is coming from a database you probably get the data as an
array already. Using doctine, it comes as an ArrayCollection object.

Then you can use JMSSerializer and add annotations and get it transformed
automatically as JSON objects using EventDispatcher in the Request/Response
cycle within Symfony2.

But if you are not using what I just mentioed and you have arrays. Keep
them as this as long as possible and use json decode only once.

An other detail is that you used

<?php
$obj = json_decode($json, true);
$result = $obj['0']->timezone;

Json_decode(JSON_STRING, true);

The true is for associative array. Not stdClass object.

Worse, var_dump() would tell you. Or var_export (not sure of the name).

Also, beware of the syntax.

$obj['0']

Will search for an associative array with a key as a sting 0.

Not the number.

PHP supports ['array','literal','notation'].  But, really is the same as
array('array,'...');

I hope it clarified things

Renoir Boulanger
https://renoirboulanger.com/#is
~
On Jun 21, 2013 3:21 PM, "Dan Parry" <dan at shaded.plus.com> wrote:

> Hi Bob
>
> Correct me if I'm wrong but you're pulling the data from a database,
> right? This means the data you're getting is most likely a string. What you
> need to use is $obj = json_encode($json);
>
> That will convert the string into the object you're expecting. If you
> still can't get $result try a var_dump() of $obj
>
> I hope I haven't misunderstood or missed anything
>
> Dan
>
> -----Original Message-----
> From: thelist-bounces at lists.evolt.org [mailto:
> thelist-bounces at lists.evolt.org] On Behalf Of Bob Meetin
> Sent: 21 June 2013 20:09
> To: thelist at lists.evolt.org
> Subject: [thelist] json_decode
>
> This seems simple enough, but it's not working. I have a json string to
> decode:
>
> <?php
> $json =
> {"admin_style":"","admin_language":"","language":"","editor":"jce","helpsite":"","timezone":"America\/Juneau"};
> // this is pulled out of a mysql table
> $obj = json_decode($json, true);
> $result = $obj['0']->timezone; // This should pull the timezone value, yes?
>
> echo "Result: $result<br />";
> ?>
>
> Of course no result, so I tried quoting the sting with single ticks.
>
> <?php
> $json =
> {"admin_style":"","admin_language":"","language":"","editor":"jce","helpsite":"","timezone":"America\/Juneau"};
> $json = "'$json'";
> $obj = json_decode($json, true);
> $result = $obj['0']->timezone;
>
> echo "Result: $result<br />";
> ?>
>
> Also tried: $result = $obj[0]->timezone; // without ticks
>
> Still no result. What am I missing?
> Bob
>
>
>
> --
>
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
> --
>
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
>


More information about the thelist mailing list