我正在尝试将字符串d解析为对象。 首先,我已经通过互联网查找试图找到解决方案,但我发现的所有内容都与JSON.stringify(d)有关,不推荐使用;和JSON.parse(d),它给了我错误:
Uncaught SyntaxError:意外的令牌o(匿名函数)@ 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"}
我不知道还有什么可以尝试,任何想法?
非常感谢
答案 0 :(得分:1)
在JSON中,字典键必须是stings,例如:
'{"id": "Whatever", "name": "Whatever", "description": "Whatever"}'