0

Possible Duplicate:
json_encode is returning NULL?

I'm trying to read JSON data from my text file 'signups.txt' and I'm using the following below. I want to grab the data from my text file and output the data in a foreach loop. Here's my code so far, I'm not 100% sure how to go about it.

$json_data = json_decode(file_get_contents('includes/signups.txt'), true);

My data:

{"name":"John Smith","studentid":"10358595","fbid":"1284556651"}

I'm thinking the following:

  • Get file data as a string Encode into JSON maybe?

  • Put the JSON data into an array using 'true'

  • And then decode so that I can use the data

Sorry for been so vague but I can't seem to find what I'm looking for online. I apologise if the post is too brief. I'll do my best to add anything that I might have missed out. Thanks.

Edit: In terms of my current code, I just get null when I var_dump it.

Community
  • 1
  • 1
  • What is wrong with your current code ? The code works wrong ad returns `$json_data` as array ??? – Baba Oct 25 '12 at 01:01
  • Just updated my situation, if I var dump what I currently have it just shows null. –  Oct 25 '12 at 01:06
  • try `echo '
    ';print_r($json_data);echo '
    ';` it gives `Array ( [name] => John Smith [studentid] => 10358595 [fbid] => 1284556651 )` here
    – davidkonrad Oct 25 '12 at 01:07
  • You can not get null .. `var_dump($json_data)` just have your code ... whats is the output ? – Baba Oct 25 '12 at 01:08
  • I just seem to get:
    –  Oct 25 '12 at 01:08
  • Is that all the data you have in `signups.txt` ??? is there is more – Baba Oct 25 '12 at 01:09
  • If I use: $json_data = file_get_contents('includes/signups.txt'); $json_output = json_decode($json_data, true); var_dump($json_data); Then it shows the data. –  Oct 25 '12 at 01:09
  • What version og PHP are you using ? – Baba Oct 25 '12 at 01:10
  • Tested on that version it works ?? – Baba Oct 25 '12 at 01:16
  • Very strange. I'm not quite sure what to do then. I'll try figure it out and then update, thanks for the help. –  Oct 25 '12 at 01:19
  • are you sure it works when you separate the variables ??? .. It would better to see your full code .. you can add it to pastebin – Baba Oct 25 '12 at 01:26
  • Here's what I currently have: http://pastebin.com/auGi421u –  Oct 25 '12 at 01:29

1 Answers1

1

From your Script http://pastebin.com/auGi421u

It shows you are having errors reading file but you don't know because you are outputting print_r($json_data); instead of print_r($json_output)

You can try this

error_reporting(E_ALL);
ini_set("display_errors", "On");

$file = 'log.txt';

if (!is_file($file) xor !is_readable($file)) {
    trigger_error("File Not readable");
}

$data = file_get_contents($file);
$data = json_decode($data, true);
var_dump($data);
Baba
  • 94,024
  • 28
  • 166
  • 217
  • I still seem to be getting the same problem. I just get NULL - using your code. –  Oct 25 '12 at 01:38
  • 5KB. I can't seem to figure it out, I'll try again in the morning, maybe I can get it working with a fresh mind. –  Oct 25 '12 at 01:49