FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » json_decode problem
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: json_decode problem [message #178844 is a reply to message #178838] Sun, 12 August 2012 11:44 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: PEAR Auth package woes
Next Topic: redirect on zend
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 03:11:17 GMT 2024

Total time taken to generate the page: 0.03537 seconds