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.

1 Answer 1

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";
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, something went wrong... Here again: it is the tree after this example: 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....
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 :)
ok, your idea with the .php probably also works... but I think in my case, a solution without an aditional file would be better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.