Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #6773

Re: json_decode problem

Message-ID <3488766.5KQix4pcSt@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-08-12 13:44 +0200
Subject Re: json_decode problem
Newsgroups comp.lang.php
References <slrnk2avb8.be8.houghi@penne.houghi> <k055v2$62k$1@dont-email.me>
Followup-To comp.lang.php

Followups directed to: comp.lang.php

Show all headers | View raw


Gregor Kofler wrote:

> Am 2012-08-10 23:30, houghi meinte:
>> <?php
>> $json = file_get_contents("imdb.json");
>> $result = json_decode($json);
>> foreach($result->data->quotes as $p)
>> {
>> $qconst = $p->qconst;
>> echo "quotenumber = $qconst<br>";
>> //Some foreach here?
>> //No idea what to put here
>> }
>> ?>
>>
>> I do get the above part right, but I am lost on how to do the rest. I
>> assume I must do some other foreach.
> 
> foreach($p->lines as $l) {
>    echo $l->stage;
> 
>    if(isset($l->quote)) {
>      echo $l->quote;
>    }
> 
>    if(isset($l->chars) {
>      foreach($l->chars as $c) {
>        echo $c->char;
>        echo $c->nconst;
>      }
>    }
> 
>    ....
> }

FYI: isset() should not be used with properties, because it fails with 
protected or private properties that have getters:

$ php -r 'class Foo { protected $_bar; public function __construct() { 
$this->_bar = 42; } public function __get($name) { return $this->_bar; } } 
$x = new Foo(); var_dump($x); var_dump($x->foo); var_dump(isset($x->foo));'
object(Foo)#1 (1) {
  ["_bar":protected]=>
  int(42)
}
int(42)
bool(false)

I have run into this problem recently in an MVC-based commercial application 
that I had written, where I was accessing a template variable (implemented 
as item of a protected array property of the view class, accessed with
$this->foo).  Not using isset() in the template would have saved me hours of 
debugging.  property_exists() would not work either from public context then 
(it would always return FALSE).

isset() usually should also not be used with a json_decode() [0] return 
value because JSON allows the `null' literal [1] as value which is parsed 
into PHP's NULL value for which isset() returns FALSE [2]:

$ php -r "var_dump(json_decode('{\"foo\": null}'));"
object(stdClass)#1 (1) {
  ["foo"]=>
  NULL
}

$ php -r "\$x = json_decode('{\"foo\": null}'); var_dump(isset(\$x->foo));"
bool(false)

Maybe is_null() or a strict comparison with NULL (whichever you prefer) 
serves in general [3]; property_exists() [4] should serve with json_decode() 
(where parsed properties are supposed to be public), unless the NULL value 
should cause an existing property to be ignored by the program logic as 
well.


PointedEars
___________
[0] <http://php.net/json_decode>
[1] <http://json.org/>
[2] <http://php.net/isset>
[3] <http://php.net/is_null>;
    <http://www.php.net/manual/en/language.operators.comparison.php>
[4] <http://php.net/property_exists>
-- 
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
  -- Richard Cornford, comp.lang.javascript, 2011-11-14

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

json_decode problem houghi <houghi@houghi.org.invalid> - 2012-08-10 23:30 +0200
  Re: json_decode problem Gregor Kofler <usenet@gregorkofler.com> - 2012-08-11 10:43 +0200
    Re: json_decode problem houghi <houghi@houghi.org.invalid> - 2012-08-11 13:09 +0200
    Re: json_decode problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-12 13:44 +0200
      Re: json_decode problem Luuk <luuk@invalid.lan> - 2012-08-12 15:28 +0200
      Re: json_decode problem Luuk <luuk@invalid.lan> - 2012-08-12 15:32 +0200
      Re: json_decode problem Gregor Kofler <usenet@gregorkofler.com> - 2012-08-12 18:12 +0200
        Re: json_decode problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-12 23:30 +0200
      Re: json_decode problem Curtis Dyer <dyer85@gmail.com> - 2012-08-20 10:41 +0000
        Re: json_decode problem Jerry Stuckle <jstucklex@attglobal.net> - 2012-08-20 07:30 -0400
        Re: json_decode problem Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-20 16:45 +0200
  Re: json_decode problem houghi <houghi@houghi.org.invalid> - 2012-08-11 10:54 +0200
    Re: json_decode problem Luuk <luuk@invalid.lan> - 2012-08-11 11:05 +0200
      Re: json_decode problem houghi <houghi@houghi.org.invalid> - 2012-08-11 13:11 +0200

csiph-web