Solved

php json_decode syntax issue

Posted on 2010-08-15
5
615 Views
Last Modified: 2012-05-10
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
Comment
Question by:cgray1223
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 33443197
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
 

Author Comment

by:cgray1223
ID: 33443310
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
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 33443343
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
 

Author Comment

by:cgray1223
ID: 33447728
Thanks Dave, I was able to get the mysql database up so I will go that route now.  Thanks for the help though.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 33447929
Glad you're able to get something working.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now