json_decode problem

  • Follow


I am stuck with json_decode. I have a json file
http://houghi.org/Fun/imdb.json
I want it to look like http://www.imdb.com/title/tt0382932/quotes
Well, not completely. Just pure text.

e.g. the second one should be something like
quotenumber = qt0465193
Remy/nm0652663 Stage=observing what Emile is eating Quote=What are you eating?
Emile/nm0812307 Stage=pause Quote=I don't really know. I think it was some sort of wrapper once.
Remy/nm0652663 Quote=What? No! You're in Paris now, baby! My town! No brother of mine eats rejecta-menta in my town!

The code I have is extremely basic. I have been looking at many
websites, but am unable to figure it out what I must put in it to make
it work:

<?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. No idea where to start, even after many days
and websites.

houghi
-- 
You can have my keyboard ...
		      if you can pry it from my dead, cold, stiff fingers
0
Reply houghi (247) 8/10/2012 9:30:16 PM

Am 2012-08-10 23:30, houghi meinte:
> I am stuck with json_decode. I have a json file
> http://houghi.org/Fun/imdb.json
> I want it to look like http://www.imdb.com/title/tt0382932/quotes
> Well, not completely. Just pure text.
>
> e.g. the second one should be something like
> quotenumber = qt0465193
> Remy/nm0652663 Stage=observing what Emile is eating Quote=What are you eating?
> Emile/nm0812307 Stage=pause Quote=I don't really know. I think it was some sort of wrapper once.
> Remy/nm0652663 Quote=What? No! You're in Paris now, baby! My town! No brother of mine eats rejecta-menta in my town!
>
> The code I have is extremely basic. I have been looking at many
> websites, but am unable to figure it out what I must put in it to make
> it work:
>
> <?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;
     }
   }

   ....
}


> No idea where to start, even after many days
> and websites.

And never heard of var_dump()?

Gregor


-- 
http://vxweb.net
0
Reply usenet2181 (203) 8/11/2012 8:43:13 AM


houghi wrote:
> I am stuck with json_decode. I have a json file
> http://houghi.org/Fun/imdb.json
> <?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
> 	}
> ?>

As always one finds the answer shortly after posting, regardless how
many hours you were looking for the solution. (30-40 over a weeks
period)

The file I use is http://houghi.org/Fun/imdb.json

<?php
$json = file_get_contents("imdb.json");
$result = json_decode($json);
foreach($result->data->quotes as $p)
{
	$qconst = $p->qconst;	
	print "quote=$qconst<br>\n";
	$qconst = $p->lines;
	foreach ($qconst as $q)
	{
		$quote = $q->quote;
		$stage = $q->stage;
		if ($stage != "") { $stage = '<i>['.$stage.']</i> ';}
		$chars = $q->chars;
		foreach ($q->chars as $r)
		{
			$char = $r->char." ";
			$nconst = $r->nconst." ";
		}
		print $char.$nconst.$stage.$quote."<br>\n";
	}
	print "<hr>\n";	
}
?>

It seems obvious now. Anybody an idea on how to improve it? I myself
will re-write the output to put the data it in a database (for personal
use).

houghi
-- 
You can have my keyboard ...
		      if you can pry it from my dead, cold, stiff fingers
0
Reply houghi (247) 8/11/2012 8:54:12 AM

On 11-08-2012 10:54, houghi wrote:
> houghi wrote:
>> I am stuck with json_decode. I have a json file
>> http://houghi.org/Fun/imdb.json
>> <?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
>> 	}
>> ?>
> 
> As always one finds the answer shortly after posting, regardless how
> many hours you were looking for the solution. (30-40 over a weeks
> period)
> 
> The file I use is http://houghi.org/Fun/imdb.json
> 
> <?php
> $json = file_get_contents("imdb.json");
> $result = json_decode($json);
> foreach($result->data->quotes as $p)
> {
> 	$qconst = $p->qconst;	
> 	print "quote=$qconst<br>\n";
> 	$qconst = $p->lines;
> 	foreach ($qconst as $q)
> 	{
> 		$quote = $q->quote;
> 		$stage = $q->stage;
> 		if ($stage != "") { $stage = '<i>['.$stage.']</i> ';}
> 		$chars = $q->chars;
> 		foreach ($q->chars as $r)
> 		{
> 			$char = $r->char." ";
> 			$nconst = $r->nconst." ";
> 		}
> 		print $char.$nconst.$stage.$quote."<br>\n";
> 	}
> 	print "<hr>\n";	
> }
> ?>
> 
> It seems obvious now. Anybody an idea on how to improve it? I myself
> will re-write the output to put the data it in a database (for personal
> use).
> 
> houghi
> 

use isset() to check if a quote of stage exists....
(see post from Gregor)
0
Reply luuk (828) 8/11/2012 9:05:11 AM

Gregor Kofler wrote:
>> 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;
>      }
>    }
>
>    ....
> }

Thanks.

>
>> No idea where to start, even after many days
>> and websites.
>
> And never heard of var_dump()?

Yes and I used it, but unfortunatly the outcome is meaningless to me.
Searching google is also very hard if you have no idea what you should
enter in the search. :-(

The sites I found that explained with examples (as that is the best way
I can learn) did not go into enough detail and were very basic.

houghi
-- 
This is written under the inluence of the following:
>   Artist : James Brown
>     Song : Instrumental Bridge 2
>    Album : Live At The Apollo
0
Reply houghi (247) 8/11/2012 11:09:52 AM

Luuk wrote:
> use isset() to check if a quote of stage exists....
> (see post from Gregor)

Indeed. Stoopid me. Thanks to pointing it out to me.

houghi
-- 
This is written under the inluence of the following:
>   Artist : Guano Apes
>     Song : Move A Little Closer
>    Album : Guano Apes live
0
Reply houghi (247) 8/11/2012 11:11:14 AM

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
0
Reply PointedEars (2103) 8/12/2012 11:44:35 AM

On 12-08-2012 13:44, Thomas 'PointedEars' Lahn wrote:
> $ php -r "\$x = json_decode('{\"foo\": null}'); var_dump(isset(\$x->foo));"
> bool(false)

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


0
Reply luuk (828) 8/12/2012 1:28:58 PM

On 12-08-2012 13:44, Thomas 'PointedEars' Lahn wrote:
> 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:
>
.....
.....
>


maybe you should not use
var_dump(isset(...));

it's pretty useless because:
isset() will always return 'true' of 'false'


0
Reply luuk (828) 8/12/2012 1:32:59 PM

Am 2012-08-12 13:44, Thomas 'PointedEars' Lahn meinte:
> 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:

Thanks, for pointing out potential problems, though the returned object 
is a stdClass instance and all properties are public.

(Anyway, I prefer to treat JSON responses as associative arrays.)

Gregor
0
Reply usenet2181 (203) 8/12/2012 4:12:42 PM

Gregor Kofler wrote:

> Am 2012-08-12 13:44, Thomas 'PointedEars' Lahn meinte:
>> 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:
> 
> Thanks, for pointing out potential problems, though the returned object
> is a stdClass instance and all properties are public.

Apparently you have missed (and snipped) the part of my answer that 
discusses when and when not to use isset() with json_decode() return values, 
and why.
 
> (Anyway, I prefer to treat JSON responses as associative arrays.)

The second argument passed to json_decode() being TRUE.  ACK.  However, that 
does not help with the isset() issue. 


PointedEars
-- 
Sometimes, what you learn is wrong. If those wrong ideas are close to the 
root of the knowledge tree you build on a particular subject, pruning the 
bad branches can sometimes cause the whole tree to collapse.
  -- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom@94.75.214.39>
0
Reply PointedEars (2103) 8/12/2012 9:30:53 PM

Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote:

> Gregor Kofler wrote:
> 
>> Am 2012-08-10 23:30, houghi meinte:

<snip>

>> 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).

According to the PHP documentation, as of PHP 5.3.0, 
`property_exists()' will also acknowledge protected and private 
properties. [1]

When using at least PHP 5.1.0, one might also consider PHP 
reflection to check for private and protected properties:

  <?php

  class Foo {
    private $test;
  }

  $r = new ReflectionObject(new Foo());
  var_dump($r->hasProperty('test')); /* bool(true) */

  ?>


<snip>

______
[1] <http://php.net/property_exists>

-- 
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>

0
Reply dyer85 (333) 8/20/2012 10:41:23 AM

On 8/20/2012 6:41 AM, Curtis Dyer wrote:
> Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote:
>
>> Gregor Kofler wrote:
>>
>>> Am 2012-08-10 23:30, houghi meinte:
>
> <snip>
>
>>> 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).
>
> According to the PHP documentation, as of PHP 5.3.0,
> `property_exists()' will also acknowledge protected and private
> properties. [1]
>
> When using at least PHP 5.1.0, one might also consider PHP
> reflection to check for private and protected properties:
>
>    <?php
>
>    class Foo {
>      private $test;
>    }
>
>    $r = new ReflectionObject(new Foo());
>    var_dump($r->hasProperty('test')); /* bool(true) */
>
>    ?>
>
>
> <snip>
>
> ______
> [1] <http://php.net/property_exists>
>

You should not need either.  Properties are private or protected for a 
reason!

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
Reply jstucklex (14410) 8/20/2012 11:30:50 AM

Curtis Dyer wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote:
>> 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).
> 
> According to the PHP documentation, as of PHP 5.3.0,
> `property_exists()' will also acknowledge protected and private
> properties. [1]

ACK:

$ php -r 'class Foo { private $bar; } var_dump(property_exists(new Foo(), 
"bar"));'
bool(true)

$ php -r 'class Foo { private $bar; } var_dump(property_exists(new Foo(), 
"baz"));'
bool(false)

$ php -r 'class Foo { protected $bar; } var_dump(property_exists(new Foo(), 
"bar"));'
bool(true)

$ php -r 'class Foo { protected $bar; } var_dump(property_exists(new Foo(), 
"baz"));'
bool(false)

$ php -v
PHP 5.3.10-1 (cli) (built: Feb  3 2012 10:03:01) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
    with XCache v1.3.2, Copyright (c) 2005-2011, by mOo
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
    with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH

One could consider that an OOP bug.  On the other hand, var_dump() also 
dumps private and protected properties from public context:

$ php -r 'class Foo { private $bar; } var_dump(new Foo());'object(Foo)#1 (1) 
{
  ["bar":"Foo":private]=>
  NULL
}

$ php -r 'class Foo { protected $bar; } var_dump(new Foo());'
object(Foo)#1 (1) {
  ["bar":protected]=>
  NULL
}

Either both functions are OOP-buggy, or none is.
 
> When using at least PHP 5.1.0, one might also consider PHP
> reflection to check for private and protected properties:
> 
>   <?php
> 
>   class Foo {
>     private $test;
>   }
> 
>   $r = new ReflectionObject(new Foo());
>   var_dump($r->hasProperty('test')); /* bool(true) */
> 
>   ?>

Overkill in most cases.


PointedEars
-- 
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann
0
Reply PointedEars (2103) 8/20/2012 2:45:56 PM

13 Replies
37 Views

(page loaded in 0.209 seconds)


Reply: