SyntaxError:JSON.parse:使用d3.js的JSON数据的第2行第14列的字符串文字中存在错误的控制字
SyntaxError: JSON.parse: bad control character in string literal at line 2 column 14 of the JSON data with d3.js
我正试图用http://bl.ocks.org/mbostock/2966094.我用json数据替换了tree.json,并对代码进行了一些修改,但它似乎不起作用。
structure.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>
tree.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"的value
在字符串中。这是一个JSON对象,不应声明为字符串。以下是正确的格式(请参阅我删除的引号(:
{
"value": {"vision":["For Commuters ... "]}
}
从我读到的评论中,当您在jsonlint
中粘贴JSON时,您得到了一个有效的JSON。那是因为jsonlint
认为你写了这样的东西:
{
"value": "another_value"
}
由于JSON对象是用引号括起来的,jsonlint
将其作为一个文本字符串。
附言:在你的标题中,你自己也提到错误在第2行第14列。计算第2行和第14列中的内容——这是您需要删除的"
字符。
相关文章:
- 如何使用JSON字符串中的jQuery填充下拉框
- Json字符串可以'当字符串末尾有“'时,t解码;
- jquery从2个json字符串构建一个复选框表单
- RegEx只匹配JSON字符串中最里面的数组
- json字符串的Javascript帖子:没有任何东西传入或返回
- 使用javascript在MVC视图中解析.net JSON字符串
- 用Angularjs+Typescript将GZip Json字符串解压为普通Json字符串
- 如何将表单数据传递给PHP并将json字符串返回给Javascript
- 如何使用AngularJS应用基于Json字符串响应的条件CSS类
- 停止JSON.parse()从JSON字符串数据中删除尾随零
- 将JSON字符串转换为函数参数
- 在AngularJS中创建JSON字符串
- 为什么我不能使用jQuery.parseJSON(json)解析json字符串
- Json字符串更改日期
- JSON字符串格式错误
- Json和$scope的角度之间的差异$eval应用于JSON字符串时
- C#JSON字符串到JavaScript数组
- 如何在JSON字符串中实现嵌套HTML
- 如何从带有多个对象的JSON字符串创建JSON对象
- 使用捕获组查找和替换json字符串中出现的所有短语