How do I add quotation marks to a JSON Object attributes for example like this:
{name:"User 01"}
so it should look like that afterward:
{"name":"User 01"}
both of them are strings
How do I add quotation marks to a JSON Object attributes for example like this:
{name:"User 01"}
so it should look like that afterward:
{"name":"User 01"}
both of them are strings
Assuming the first example is a Javascript object, you could convert it into a JSON string using JSON.stringify:
JSON.stringify({name:"User 01"});
outputs: "{"name":"User 01"}"
If the first example is a string, I think you would have to parse through it with methods like split.
JSON.stringify(eval('{name:"User 01"}'));
Not really great but works.
the first notation
var string = {name:"user 01"}
if you use it then you can directly access all the properties and methods of the string object
but if you use this notation :
var string = {"name":"user 01"}
then you have to use :
window.JSON.parse("'"+string+"'")
Update: Now that we have ES6, you can use template literals :
window.JSON.parse(`'${string}'`)
in order to access all the methods and properties of string object
the last notation is used generally when getting data back from php script or something like that
var obj = { "name": "user 01" }? That's a perfectly-valid JS object literal.. the quotation marks around the key are optional if the key is a valid identifier, but always acceptable.