Solved

php json_decode syntax issue

Posted on 2010-08-15
5
624 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 83

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 83

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 83

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

932 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