PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > JSON member access issue
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
JSON member access issue

Réponse
 
LinkBack Outils de la discussion
Vieux 22/04/2008, 19h32   #1
Logos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut JSON member access issue

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<br />");
}
//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
  Réponse avec citation
Vieux 22/04/2008, 23h14   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: JSON member access issue

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=>$value) {
> echo("$key<br />");
> }
> //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
  Réponse avec citation
Vieux 23/04/2008, 21h16   #3
Logos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: JSON member access issue

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<br />");
> > }
> > //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
  Réponse avec citation
Vieux 24/04/2008, 02h18   #4
Alexey Kulentsov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: JSON member access issue

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};
  Réponse avec citation
Vieux 24/04/2008, 17h29   #5
Logos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: JSON member access issue

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!!!



Tyler
  Réponse avec citation
Vieux 30/04/2008, 02h39   #6
Dave Benjamin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: JSON member access issue

Logos 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"].


Looks like you're already on your way, but just FYI, the json_decode
function takes an optional second argument that will, if true, cause
objects to be returned as associative arrays instead. Then, the usual
array notation (["1"]) should work.

Dave
  Réponse avec citation
Vieux 30/04/2008, 10h11   #7
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: JSON member access issue

On Wed, 30 Apr 2008 03:39:16 +0200, Dave Benjamin
<ramen@lackingtalent.com> wrote:

> Logos 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"].

>
> Looks like you're already on your way, but just FYI, the json_decode
> function takes an optional second argument that will, if true, cause
> objects to be returned as associative arrays instead. Then, the usual
> array notation (["1"]) should work.


D'OH! Going through all that trouble writing a recursive function...
--
Rik Wasmus
  Réponse avec citation
Vieux 02/05/2008, 20h10   #8
Logos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: JSON member access issue

On Apr 29, 6:39 pm, Dave Benjamin <ra...@lackingtalent.com> wrote:
> Logos 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"].

>
> Looks like you're already on your way, but just FYI, the json_decode
> function takes an optional second argument that will, if true, cause
> objects to be returned as associative arrays instead. Then, the usual
> array notation (["1"]) should work.
>
> Dave


Keen - I may try that too. Good to know for future reference, in any
event!

Tyler
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h31.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18960 seconds with 16 queries