?
Solved

php json_decode syntax issue

Posted on 2010-08-15
5
Medium Priority
?
678 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 84

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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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 84

Expert Comment

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

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

752 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