Reputation: 101
I have a function working fine:
function sendLeadData(form) {
return trk(form, {
firstName: "pan",
});
}
{firstName : "Pan"}
= an object. If I set this to a var and pass the var in function that also works fine. But I need to use a string which I am building from some map and pass that. I am getting the string perfect .Code below not working:
function sendLeadData(form) {
//code to get str
alert("str is "+str);------->prints str perfect,also giving result below
var obj = JSON.parse(str);//If except str I put JSON.parse(JSON.stringify({firstName:"Pan"})) that work fine
alert("obj is "+obj);
return trk(form,
obj
);
}
str prints "{firstName:"Pan"}". Error is syntax error thrown.
Please help.
Upvotes: 0
Views: 68
Reputation: 4370
var obj = {x: 5, y: 6, apple:0, name:'myname'};
var objToText = JSON.stringify(obj);
console.log(objToText);
var text = '{"x": 5, "y": 6, "apple":0, "name":"myname"}';
var textToObj = JSON.parse(text);
console.log(textToObj);
Upvotes: 0
Reputation: 33399
JSON has more strict object representation. The keys also need to be quoted:
'{"firstName": "Pan"}'
Upvotes: 4