【发布时间】:2015-09-22 17:22:11
【问题描述】:
我正在尝试将字符串 d 解析为一个对象。 首先,我已经通过互联网查找了试图找到解决方案,但我发现的都是与 JSON.stringify(d) 相关的,它已被弃用;和 JSON.parse(d),这给了我错误:
Uncaught SyntaxError: Unexpected token o(anonymous function) @ map.js:286(匿名函数)@d3.v3.min.js:1
代码如下:(注释行是我的一些不同的尝试)
apartmentinfo.append('p')
.append('a')
.attr('class', 'descriptionbutton buttonleft')
.on('click', function () {
//javascript convert string into object
//var json = JSON.stringify(d);
//var descrip = eval('(' + d + ')');
//var apart = JSON.parse(d);
changeApartmentInfo(d);
viewSingleApartment();
})
.text("Description");
每当我使用 console.log(d) 打印 d 时,控制台中都会出现:
Object{ id: "Whatever", name: "Whatever", description: "Whatever", piso_url: "Whatever", piso_imagen: "Whatever"… (HERE I HAVE AN INFO ICON THAT SAYS: Object state below is captured after first expansion)
description: "Whatever"
host_id: "Whatever"
host_name: "Whatever"
id: "Whatever"
latitude: "Whatever"
licencia: "Whatever"
longitude: "Whatever"
name: "Whatever"
piso_imagen: "Whatever"
piso_url: "Whatever"
price: "Whatever"
red: "Whatever"
room_type: "Whatever"}
我不知道还能尝试什么,有什么想法吗?
非常感谢
【问题讨论】:
-
向我们展示这个对象。你也可以使用一些在线的 JSON 验证器来检查这个字符串是否正确
-
我已经用对象编辑了评论
-
d中有{和}吗?另外,不要...文件。开始和结束可能是破坏它的原因。 -
JSON.stringify()何时被弃用?
标签: javascript json string parsing d3.js