简体   繁体   中英

D3 unexpected value translate, won't accept variable

I have a weird error where D3 won't take my JSON data referenced by a variable, but will take it if I print it out through the console log and copy-paste it into the same variable.

So this source of JSON doesn't work:

function reformat(data) {

var jsonObject = JSON.parse(data, function(k, v) {
    return (typeof v === "object" || isNaN(v)) ? v : parseInt(v, 10);
});

var treeData = (JSON.stringify(makeTree(jsonObject), null, 4));
//etc code for D3 
}

But If I literally print it through the console, and declare it as such:

treeData = [
    {
        "id": 1,
        "name": "Start",
        "children": [
            {
                "id": 2,
                "name": "Decision A",
                "children": [
                    {
                        "id": 4,
                        "name": "Possibility A-1",
                        "children": [
                            {
                                "id": 8,
                                "name": "Consequence A-1"
                            }
                        ]
                    },
                    {
                        "id": 5,
                        "name": "Possibility A-2",
                        "children": [
                            {
                                "id": 9,
                                "name": "Consequence A-2"
                            }
                        ]
                    }
                ]
            },
            {
                "id": 3,
                "name": "Decision B",
                "children": [
                    {
                        "id": 6,
                        "name": "Possibility B-1",
                        "children": [
                            {
                                "id": 10,
                                "name": "Consequence B-1"
                            }
                        ]
                    },
                    {
                        "id": 7,
                        "name": "Possibility B-2",
                        "children": [
                            {
                                "id": 11,
                                "name": "Consequence B-2"
                            }
                        ]
                    }
                ]
            }
        ]
    }
];

Suddenly works fine. I can't keep copying and re-declaring the variable. If the output is identical, why doesn't it accept it?

The function gets called out once upon loading the web page:

 function reqListener () {
      console.log(this.responseText);
    }

    var oReq = new XMLHttpRequest(); 
    oReq.onload = function() {

       reformat(this.responseText);
    };
    oReq.open("get", "getData.php", true);

    oReq.send();

Why do you stringify the JSON and pass it into treeData?

Instead of

var treeData = (JSON.stringify(makeTree(jsonObject), null, 4));

do this

var treeData = makeTree(jsonObject);

This should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM