0

I have a nice working d3.js-tree as long as I use JSON-data from a .json-file. But now, I want to give the opportunity to the customer to individualize the tree in some ways. For me it seems logical therefore, to use the SESSION to hold the respective JSON-String and to load this SESSION-variable into my tree-script.

Therefore, I use window.SESSION like this and parse the string to JSON.

 var treedatajson = JSON.parse(window.SESSION.filtered_tree);

But this seems not to work. The string must be ok, as I can copy it into a file and load a functioning tree with the following code:

 var treedatajson = "filtered_tree.json";

Why doesn't JSON.parse() work that way? And what can I do to get this running?

Any idea? Thanks in advance!

Let me know if there is something unclear or you need more code for context.

Maki
  • 109
  • 13

1 Answers1

1

looks like in var treedatajson = "filtered_tree.json"; when it worked you were passing in the filename, and in var treedatajson = JSON.parse(window.SESSION.filtered_tree); you were passing in the json data. What is the next line where you use treedatajson? You are probably looking for a filename there instead of the parsed data.

... or maybe you should create a php endpoint that serves the session data as json... something like

<?php
// filtered-tree.php
session_start()
header('Content-Type: application/json');
echo json_encode($_SESSION['TREE']); // or whatever the key is

and then in js

var treedatajson = "filtered-tree.php";
Alex028502
  • 3,486
  • 2
  • 23
  • 50
  • Sorry, something went wrong... Here again: it is the tree after this example: https://gist.github.com/robschmuecker/7880033 And the next line is the function for creating the tree and its routines: treeJSON = d3.json(treedatajson, function(error, treeData) { To be honest, I don't know whether it only refers to the file/path or directly uses the json.... I'll try to change things to check that.... – Maki Feb 25 '18 at 14:04
  • this might help https://stackoverflow.com/questions/15764698/loading-d3-js-data-from-a-simple-json-string – Alex028502 Feb 25 '18 at 14:08
  • you're my hero :D I overwrite the treeData within the function with my parsed JSON-string, which works very well. Thanks for the link :) – Maki Feb 25 '18 at 14:14
  • ok, your idea with the .php probably also works... but I think in my case, a solution without an aditional file would be better. – Maki Feb 25 '18 at 14:17