gpt4 book ai didi

javascript - 语法错误: JSON.解析: bad control character in string literal at line 2 column 14 of the JSON data with d3. js

转载 作者:行者123 更新时间:2023-12-03 09:41:06 27 4
gpt4 key购买 nike

我试图获得与 http://bl.ocks.org/mbostock/2966094 类似的结构。我用我的 json 数据替换了 tree.json 并稍微修改了代码,但它似乎不起作用。

结构.html

    <!DOCTYPE html>
<meta charset="utf-8">
<style>

text {
font-family: "Helvetica Neue", Helvetica, sans-serif;
}

.name {
font-weight: bold;
}

.about {
fill: #777;
font-size: smaller;
}

.link {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}

</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>

var margin = {top: 0, right: 320, bottom: 0, left: 0},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

var tree = d3.layout.tree()
.separation(function(a, b) { return a.parent === b.parent ? 1 : .5; })
.children(function(d) { return d.parents; })
.size([height, width]);

var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.json("tree.json", function(error, json) {
if (error) throw error;

var nodes = tree.nodes(json);

var link = svg.selectAll(".link")
.data(tree.links(nodes))
.enter().append("path")
.attr("class", "link")
.attr("d", elbow);

var node = svg.selectAll(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })

node.append("text")
.attr("class", "name")
.attr("x", 8)
.attr("y", -6)
.text(function(d) { return d.vision; });

node.append("text")
.attr("class", "name")
.attr("x", 8)
.attr("y", -6)
.text(function(d) { return d.actor; });

/* node.append("text")
.attr("x", 8)
.attr("y", 8)
.attr("dy", ".71em")
.attr("class", "about lifespan")
.text(function(d) { return d.born + "–" + d.died; });*/

/* node.append("text")
.attr("x", 8)
.attr("y", 8)
.attr("dy", "1.86em")
.attr("class", "about location")
.text(function(d) { return d.location; });*/
});

function elbow(d, i) {
return "M" + d.source.y + "," + d.source.x
+ "H" + d.target.y + "V" + d.target.x
+ (d.target.children ? "" : "h" + margin.right);
}

</script>

树.json

{
"value": "{\"vision\":[\"For Commuters who Spend hell lot of time in traffic is a Traditional Commuting solution that Tracking enabled mobile App unlike Sharing the rides our product Car pooling \",\"For Commuters who Spend hell lot of time in traffic is a Traditional Commuting solution that Tracking enabled mobile App unlike Sharing the rides our product Car pooling \"],\"actors\":[\"Commuters\",\"Government\"]}"
}

最佳答案

观察您的 JSON:

{
"value": "{"vision":["For Commuters ... "]}"
}

您的“value”的是字符串形式的。这是一个 JSON 对象,不应声明为字符串。这是正确的格式(请参阅我删除了引号):

{
"value": {"vision":["For Commuters ... "]}
}

从评论中我读到,当您将 JSON 粘贴到 jsonlint 中时,您会发现它是有效的 JSON。那是因为 jsonlint 认为您写了这样的内容:

{
"value": "another_value"
}

由于您的 JSON 对象包含在引号中,因此 jsonlint 将其视为文字字符串。

PS:在您的标题中,您自己提到错误出现在第 2 行第 14 列中。我们来数一下第 2 行和第 14 列中的内容 - 这是您需要删除的 " 字符。

关于javascript - 语法错误: JSON.解析: bad control character in string literal at line 2 column 14 of the JSON data with d3. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31184312/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com