Solved

php json_decode syntax issue

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

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!

Question has a verified solution.

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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 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