[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 703
  • Last Modified:

php json_decode syntax issue

Hello,

I have a json object structure in a file and I'm trying to load that in my php script in order to make an array out of it and perform some operations on that array.  I'm unable to get the json_decode method to work.  My first two alerts come out but "here3" never gets echoed out.  Can someone see an issue with my syntax?

php code:
<?php
	echo "here1";
	$file_content = file_get_contents('./pic_conf.json', true);
	echo "here2";
	$conf = (array)json_decode($file_content, true);
	echo "here3";
?>

Open in new window


pic_config.json contents:
{"a":1,"b":2,"c":3,"d":4,"e":5}

Open in new window




0
cgray1223
Asked:
cgray1223
  • 3
  • 2
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
I had to rename the json file as 'picconfig.txt' because my server wouldn't 'serve' it with a 'json' extension.  I put it in the same directory as the PHP file and this worked.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>json_decode</title>
</head>
<body>
<?php
	echo "here1<br>";
	$file_content = file_get_contents('picconfig.txt');
	echo "here2<br>";
	$conf = (array)json_decode($file_content, true);
	echo "here3<br>";
	print_r($conf);
?>
</body>
</html>

Open in new window

0
 
cgray1223Author Commented:
problem is I'm on php 5.1.6 and the json_decode is in 5.2.0.  I'm running this site on dot easy so I'll have to figure out how to upgrade if I want to use this function.  Thanks for the help though!
0
 
Dave BaldwinFixer of ProblemsCommented:
Here's a library that claims to work with 5.1.4 or greater: http://thecodecentral.com/2007/09/13/easy-json-encodingdecoding-in-php
0
 
cgray1223Author Commented:
Thanks Dave, I was able to get the mysql database up so I will go that route now.  Thanks for the help though.
0
 
Dave BaldwinFixer of ProblemsCommented:
Glad you're able to get something working.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now