简体   繁体   中英

Node.js JSON.parse(string) is returning a string

I have a JSON object as a string, I am parsing it with JSON.parse() but the resulting object is still a string. Am I doing something wrong?

var myString = "{Username:Brad,Password:12345}";

// adding in the quotes or else it throws an error saying 'unidentified token U
var myJson = JSON.parse('"' + myString + '"');

console.log(myJson.Username); // prints 'undefined'
console.log(typeof(myJson));  // prints 'string'

That's not valid JSON. keys and strings need to be quoted:

var myString = '{"Username":"Brad","Password":12345}';
var myJson = JSON.parse( myString );

See json.org for information about JSON.

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