basickarl
basickarl

Reputation: 40522

PHP: var_dump spitting out wrong format from json_decode

My code:

$json_data = file_get_contents($json_url,0,null,null);  
$json_output = json_decode($json_data); 
var_dump($json_output);

This is what var_dump spits out in one big line, not formatted at all:

object(stdClass)#1 (1) { ["timetableresult"]=> object(stdClass)#2 (1) { ["ttitem"]=> array(17) { [0]=> object(stdClass)#3 (1) { ["segment"]=> array(8) { [0]=> object(stdClass)#4 (3) { ["segmentid"]=> object(stdClass)#5 (2) { ["mot"]=> object(stdClass)#6 (3) { ["@displaytype"]=> string(1) "G" ["@type"]=> string(1) "G" ["#text"]=> string(5) "GÃ¥ng" } ["distance"]=> int(1008) } ["departure"]=> object(stdClass)#7 (2)... etc.etc.

What am I doing wrong?

Upvotes: 1

Views: 1961

Answers (2)

Kyle Buser
Kyle Buser

Reputation: 363

You're doing a var dump, to access the properties you would do something like this: $seg_id = $json_output->timetableresult->ttitem->segment[0]->segmentid; I didn't actually look closely at the structure of your json, but that is approximately what you'll want.

Or... If you do this foreach($json_output->timetableresult->ttitem as $item) { var_dump($item); }

You'll begin to see how to access all the parts you want.

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

var_dump prints newlines. Browsers do not render newlines.

Try echo "<pre>"; var_dump($json_output); echo "</pre>";

Upvotes: 6

Related Questions