简体   繁体   中英

Getting an error on JSON.parse

I was reading an article by Derick Bailey Don't Return A JSON Document From The toJSON Method

I was trying to convert the following JSON object into Javascript:

var userJSON = "{\"firstName\":\"Derick\",\"lastName\":\"Bailey\"}";
var parseOnce = JSON.parse(userJSON.to_json).first;
var userObject = JSON.parse(parseOnce.to_json).first;

When I run the code I get a "SyntaxError: Unexpected token u"

Here is a jsbin of the same code https://jsbin.com/zugojoyaro/edit?js,console

this is a copy & paste problem from the blog post... (and a small error in the blog post, itself)


var userJSON = "{\"firstName\":\"Derick\",\"lastName\":\"Bailey\"}";

var parseOnce = JSON.parse(userJSON);

this works... but the code you have:

var parseOnce = JSON.parse(userJSON.to_json).first;

includes a .to_json and a .first attribute that don't exist. it looks like your trying to use ruby code on a JavaScript object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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