I have the following:
var cookie = $.cookie("Test_cookie");
var items = cookie ? eval("([" + cookie + "])") : [];
var jsonObj = { packageId: "11", machineId: "1", operationType: "Download" };
items.push(jsonObj);
$.cookie(cookieName, JSON.stringify(items), { expires: 1, path: '/' });
result:
[{"packageId":"11","machineId":"1","operationType":"Download"}]
which is correct.
However, when I run it the second time I want to append new object to items but json gets messed up (notice extra "["):
var jsonObj = { packageId: "11", machineId: "1", operationType: "Download" };
items.push(jsonObj);
$.cookie(cookieName, JSON.stringify(items), { expires: 1, path: '/' });
result:
[[{"packageId":"11","machineId":"1","operationType":"Download"}],{"packageId":"11","machineId":"1","operationType":"Download"}]
and it should be:
[{"packageId":"11","machineId":"1","operationType":"Download"},{"packageId":"11","machineId":"1","operationType":"Download"}]
what gives?
eval()
– Roko C. Buljan Feb 28 '13 at 14:23console.log(items)
and paste output here. – ATOzTOA Feb 28 '13 at 14:24JSON.parse
? – Ian Feb 28 '13 at 14:28