简体   繁体   English

json.parse问题

[英]Problems with json.parse

I'm trying to add json string to my d3.js app 我正在尝试将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);

It says: 它说:

Uncaught SyntaxError: Unexpected token ILLEGAL

EDIT: I've added utf-8, result is the same 编辑:我添加了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">

Why? 为什么? Also, here is fiddle . 另外,这是小提琴 EDIT Now it is one string: says: 编辑现在是一个字符串:说:

Uncaught ReferenceError: data is not defined 
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);

This will overcome the error. 这将克服该错误。 It's because of the line breaks for each statement. 这是因为每个语句的换行符。 YOu need to escape it with \\ . 您需要使用\\进行转义。

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

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