Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

nrao wwwxxx, xxxxxdup, procmail change subject header, wwwXxx not20, Wwwxxx.doks sas, linux raid resync after reboot, bind-address mysql multiple, sanibleone xxxx, ftp://192.168.100.100/, www.xxxcon

Links

XODOX
Impressum

#1: JSON member access issue

Posted on 2008-04-22 20:32:28 by Logos

I am using PHP with the JSON extension function json_decode.

I have a JSON with a member named "1" (ie) { "1":"somedata" }

Trying to access this via the -> operator doesn't work, nor does
["1"].

Putting the JSON into a foreach loop DOES access the member:

foreach($json as $key=>$value) {
echo("$key
");
}
//outputs '1'

Is this an error on my part, an oversight in the PHP JSON
implementation, or something else? Why can foreach grab the members,
but I can't access them?

Thanks for your time!

Tyler

Report this message

#2: Re: JSON member access issue

Posted on 2008-04-23 00:14:24 by luiheidsgoeroe

On Tue, 22 Apr 2008 20:32:28 +0200, Logos <tyler.style@gmail.com> wrote:=


> I am using PHP with the JSON extension function json_decode.
>
> I have a JSON with a member named "1" (ie) { "1":"somedata" }
>
> Trying to access this via the -> operator doesn't work, nor does
> ["1"].
>
> Putting the JSON into a foreach loop DOES access the member:
>
> foreach($json as $key=3D>$value) {
> echo("$key
");
> }
> //outputs '1'
>
> Is this an error on my part, an oversight in the PHP JSON
> implementation, or something else? Why can foreach grab the members,
> but I can't access them?


The problem is that while json_decode is able to extract it:
object(stdClass)#1 (1) {
["1"]=3D>
string(8) "somedata"
}

"1" is not a valid property name to use directly.

Workarounds:
Option 1, suitable for single known variable:
<?php
$var =3D json_decode('{ "1":"somedata" }');
$name =3D '1';
echo $var->$name;
?>

Option 2, suited for more generic processing:
<?php
function json_object_to_named_array($var){
if(!is_object($var)){
trigger_error('No object given');
return;
}
$return =3D get_object_vars($var);
foreach($return as &$value){
if(is_object($value)) $value =3D json_object_to_named_array($value);
}
return $return;
}
$test =3D array('foo' =3D> 'bar','foz' =3D> array('fox' =3D> 'bax'));
$json =3D json_encode($test);
var_dump($json);
$var =3D json_decode($json);
var_dump($var);
$var =3D json_object_to_named_array($var);
var_dump($var);
?>
Output:
string(33) "{"foo":"bar","foz":{"fox":"bax"}}"
object(stdClass)#1 (2) {
["foo"]=3D>
string(3) "bar"
["foz"]=3D>
object(stdClass)#2 (1) {
["fox"]=3D>
string(3) "bax"
}
}
array(2) {
["foo"]=3D>
string(3) "bar"
["foz"]=3D>
array(1) {
["fox"]=3D>
string(3) "bax"
}
}
-- =

Rik Wasmus

Report this message

#3: Re: JSON member access issue

Posted on 2008-04-23 22:16:11 by Logos

On Apr 22, 3:14 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Tue, 22 Apr 2008 20:32:28 +0200, Logos <tyler.st...@gmail.com> wrote:
> > I am using PHP with the JSON extension function json_decode.
>
> > I have a JSON with a member named "1" (ie) { "1":"somedata" }
>
> > Trying to access this via the -> operator doesn't work, nor does
> > ["1"].
>
> > Putting the JSON into a foreach loop DOES access the member:
>
> > foreach($json as $key=>$value) {
> > echo("$key
");
> > }
> > //outputs '1'
>
> > Is this an error on my part, an oversight in the PHP JSON
> > implementation, or something else? Why can foreach grab the members,
> > but I can't access them?
>
> The problem is that while json_decode is able to extract it:
> object(stdClass)#1 (1) {
> ["1"]=>
> string(8) "somedata"
>
> }
>
> "1" is not a valid property name to use directly.
>
> Workarounds:
> Option 1, suitable for single known variable:
> <?php
> $var = json_decode('{ "1":"somedata" }');
> $name = '1';
> echo $var->$name;
> ?>
>
> Option 2, suited for more generic processing:
> <?php
> function json_object_to_named_array($var){
> if(!is_object($var)){
> trigger_error('No object given');
> return;
> }
> $return = get_object_vars($var);
> foreach($return as &$value){
> if(is_object($value)) $value = json_object_to_named_array($value);
> }
> return $return;}
>
> $test = array('foo' => 'bar','foz' => array('fox' => 'bax'));
> $json = json_encode($test);
> var_dump($json);
> $var = json_decode($json);
> var_dump($var);
> $var = json_object_to_named_array($var);
> var_dump($var);
> ?>
> Output:
> string(33) "{"foo":"bar","foz":{"fox":"bax"}}"
> object(stdClass)#1 (2) {
> ["foo"]=>
> string(3) "bar"
> ["foz"]=>
> object(stdClass)#2 (1) {
> ["fox"]=>
> string(3) "bax"
> }}
>
> array(2) {
> ["foo"]=>
> string(3) "bar"
> ["foz"]=>
> array(1) {
> ["fox"]=>
> string(3) "bax"
> }}
>
> --
> Rik Wasmus

Ah, I thought it might be something like that then. The JSON notation
is perfectly fine, but PHP's grammar won't let me directly access the
incompatible JSON format. I shall just have to live with it then.

Thanks!

Tyler

Report this message

#4: Re: JSON member access issue

Posted on 2008-04-24 03:18:14 by Alexey Kulentsov

Logos wrote:

> Ah, I thought it might be something like that then. The JSON notation
> is perfectly fine, but PHP's grammar won't let me directly access the
> incompatible JSON format. I shall just have to live with it then.

:)

Try echo $var->{1};

Report this message

#5: Re: JSON member access issue

Posted on 2008-04-24 18:29:38 by Logos

On Apr 23, 6:18 pm, Alexey Kulentsov <a...@inbox.ru> wrote:
> Logos wrote:
> > Ah, I thought it might be something like that then. The JSON notation
> > is perfectly fine, but PHP's grammar won't let me directly access the
> > incompatible JSON format. I shall just have to live with it then.
>
> :)
>
> Try echo $var->{1};

YOU, sirrah, are my HERO! Thank you much much much!!!

:D

Tyler

Report this message