Johan
Johan

Reputation: 79

php json to javascript json format is not coming right as required result

i am trying to achieve below json Sorry to say but i am not getting which is issue.

required JSON screen shot with console : https://i.sstatic.net/6fzMx.jpg

var requiredJson = {
        chart: {
            container: "#OrganiseChart-simple"
        },      
        nodeStructure: {text:{name:"Root"},children:[{text:{name:"naresh"},children:[{text:{name:"rahul1"}},{text:{name:"rahul123"}},{text:{name:"kapil1"},children:[{text:{name:"priya12"},children:[{text:{name:"amit12"}}]}]}]},{text:{name:"roshan"}},{text:{name:"Seppl"}},{text:{name:"pankaj1910"}}]}
    };

My try:

my try is giving me this result: https://i.sstatic.net/d1XXD.jpg

my php json when i am using echo to json_encode($tree) :

[{"text":{"name":"Root"},"children":[{"text":{"name":"naresh"},"children":[{"text":{"name":"rahul1"}},{"text":{"name":"rahul123"}},{"text":{"name":"kapil1"},"children":[{"text":{"name":"priya12"},"children":[{"text":{"name":"amit12"}}]}]}]},{"text":{"name":"roshan"}},{"text":{"name":"Seppl"}},{"text":{"name":"pankaj1910"}}]}] ;

//$tree is a php array

var mytry = '<?php echo json_encode($tree); ?>';
 var requiredJson  = {
        chart: {
            container: "#OrganiseChart-simple"
        },

        nodeStructure: mytry 
    };

JS FIDDLE: https://jsfiddle.net/ueo0k9ys/1/

Upvotes: 1

Views: 23

Answers (1)

charlietfl
charlietfl

Reputation: 171698

Remove the quotes around the php output. They are telling the javascript compiler it is a string not array

var mytry = <?php echo json_encode($tree); ?>;
var requiredJson  = {
    chart: {
        container: "#OrganiseChart-simple"
    },

    nodeStructure: mytry 
};

Upvotes: 2

Related Questions