我正在尝试将json字符串添加到我的d3.js应用
var cityDivisionJSON='[{"city":"Челябинск","percentage":"66.67"},{"city":"Аша","percentage":"16.67"},{"city":"Бакал","percentage":"16.67"},{"city":"Верхний Уфалей","percentage":"0"},{"city":"Еманжелинск","percentage":"0"},{"city":"Златоуст","percentage":"0"},{"city":"Карабаш","percentage":"0"},{"city":"Карталы","percentage":"0"},{"city":"Касли","percentage":"0"},{"city":"Катав-Ивановск","percentage":"0"},{"city":"Коркино","percentage":"0"},{"city":"Куса","percentage":"0"},{"city":"Кыштым","percentage":"0"},{"city":"Магнитогорск","percentage":"0"},{"city":"Миасс","percentage":"0"},{"city":"Миньяр","percentage":"0"},{"city":"Нязепетровск","percentage":"0"},{"city":"Сатка","percentage":"0"},{"city":"Сим","percentage":"0"},{"city":"Снежинск","percentage":"0"},{"city":"Трехгорный","percentage":"0"},{"city":"Троицк","percentage":"0"},{"city":"Усть-Катав","percentage":"0"},{"city":"Чебаркуль","percentage":"0"},{"city":"Южноуральск","percentage":"0"},{"city":"Юрюзань","percentage":"0"}]';
root=JSON.parse(cityDivisionJSON);
var arcs=group.selectAll(".arc")
.data(pie(data))
.enter()
.append("g")
.attr("class","arc")
.on("mouseover",toggleArc)
.on("mouseleave",toggleArc)
.append("path")
.attr("d",arc)
.attr("fill",function(d){return color(d.data.percentage);});
group
.append("circle")
.style("fill","white")
.attr("r",radius-20);
它说:
Uncaught SyntaxError: Unexpected token ILLEGAL
修改 我添加了utf-8,结果是一样的
<html lang="ru">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/jquery.cookie.js"></script>
<link rel="stylesheet" href="css/styles.css" type="text/css">
<link href="css/bootstrap.css" rel="stylesheet" media="screen">
为什么呢? 此外,这里是fiddle。 的修改 现在它是一个字符串: 表示:
Uncaught ReferenceError: data is not defined
答案 0 :(得分:1)
var cityDivision = '[\
{"city":"Челябинск","percentage":"66.67"},\
{"city":"Аша","percentage":"16.67"},\
{"city":"Бакал","percentage":"16.67"},\
{"city":"Верхний Уфалей","percentage":"0"},\
{"city":"Еманжелинск","percentage":"0"},\
{"city":"Златоуст","percentage":"0"},\
{"city":"Карабаш","percentage":"0"},\
{"city":"Карталы","percentage":"0"},\
{"city":"Касли","percentage":"0"},\
{"city":"Катав-Ивановск","percentage":"0"},\
{"city":"Коркино","percentage":"0"},\
{"city":"Куса","percentage":"0"},\
{"city":"Кыштым","percentage":"0"},\
{"city":"Магнитогорск","percentage":"0"},\
{"city":"Миасс","percentage":"0"},\
{"city":"Миньяр","percentage":"0"},\
{"city":"Нязепетровск","percentage":"0"},\
{"city":"Сатка","percentage":"0"},\
{"city":"Сим","percentage":"0"},\
{"city":"Снежинск","percentage":"0"},\
{"city":"Трехгорный","percentage":"0"},\
{"city":"Троицк","percentage":"0"},\
{"city":"Усть-Катав","percentage":"0"},\
{"city":"Чебаркуль","percentage":"0"},\
{"city":"Южноуральск","percentage":"0"},\
{"city":"Юрюзань","percentage":"0"}\
]';
root = JSON.parse(cityDivision);
这将克服错误。这是因为每个语句都有换行符。你需要用\
来逃避它。