简体   繁体   English

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

I am trying to get the similar structure with http://bl.ocks.org/mbostock/2966094 . 我正在尝试通过http://bl.ocks.org/mbostock/2966094获得类似的结构。 I replaced tree.json with my json data and modified the code a bit but it does not seem to work. 我用json数据替换了tree.json,并对代码进行了一些修改,但是似乎不起作用。

structure.html 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 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\"]}"
}

Observe your JSON: 观察您的JSON:

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

Your value of "value" is in string. 你的value “价值”是字符串。 This is a JSON object and should not be declared as a string. 这是一个JSON对象,不应声明为字符串。 Here is the right format (see I removed the quotes): 这是正确的格式(请参阅我删除引号):

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

From comments I read that when you pasted your JSON in jsonlint you got that its a valid JSON. 从评论中我读到 ,当您将JSON粘贴到jsonlint您会得到一个有效的JSON。 That's because jsonlint thought that you wrote something like this: 这是因为jsonlint认为您编写了这样的内容:

{
    "value": "another_value"
}

Since your JSON object was in quotes, jsonlint took it as a literal string. 由于您的JSON对象用引号引起来,因此jsonlint将其作为文字字符串。

PS: In your heading you yourself mentioned that the error is in line2, column 14. Well count what is in line 2 and column 14 - its your " character which you needed to remove. PS:在你的标题,你自己提到,误差在2号线,14列算好什么是在第2行和列14 -是你的" ,你需要删除字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 D3.js和Drupal-SyntaxError:JSON.parse:JSON数据的第1行第1行出现意外字符 - D3.js and Drupal - SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data Selenium IDE 给出 SyntaxError: JSON.parse: bad control character in string literal - Selenium IDE gives SyntaxError: JSON.parse: bad control character in string literal JSON.parse:字符串文字中的错误控制字符 - JSON.parse : Bad control character in string literal SyntaxError:JSON.parse:JSON数据的第1行第1列出现意外字符吗? - SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data? 语法错误json.parse json数据第1行第1列的意外字符 - syntaxerror json.parse unexpected character at line 1 column 1 of the json data 语法错误:JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符 - SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data SyntaxError:JSON.parse:JSON数据第3行第1列的意外字符 - SyntaxError: JSON.parse: unexpected character at line 3 column 1 of the JSON data SyntaxError:JSON.parse:JSON数据的第1行第2列出现意外字符 - SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data 如何解决 JSON 解析错误“JSON.parse: 字符串文字中的错误控制字符”? - How can I resolve JSON parsing error 'JSON.parse: bad control character in string literal'? SyntaxError: JSON.parse: JSON 第 1 行第 2 列的意外字符 - SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM