简体   繁体   English

我无法在JavaScript中将对象从字符串解析为对象

[英]I cannot parse an object from string to object in javascript

I am trying to parse the string d to an object. 我正在尝试将字符串d解析为一个对象。 First of all, I have already looked up through the Internet trying to find a solution, but all I found is related to JSON.stringify(d), which is deprecated; 首先,我已经在Internet上查找了试图找到解决方案的方法,但是我发现的所有内容都与JSON.stringify(d)有关,已过时; and to JSON.parse(d), which gives me the error: 并转到JSON.parse(d),这给了我错误:

Uncaught SyntaxError: Unexpected token o(anonymous function) @ map.js:286(anonymous function) @ d3.v3.min.js:1 未捕获到的SyntaxError:意外的令牌o(匿名函数)@ map.js:286(匿名函数)@ d3.v3.min.js:1

The code is the following: (the commented lines are some of my different trials) 代码如下:(注释行是我的一些不同尝试)

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");

And whenever I use console.log(d) to print d, this appears in the console: 每当我使用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"}

I don't know what else to try, any idea? 我不知道还能尝试什么,知道吗?

Thank you very much 非常感谢你

在JSON中,字典键必须为st,例如:

'{"id": "Whatever", "name": "Whatever", "description": "Whatever"}'

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

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