javascript - JSON.stringify turned the value array into a string -
i have js object
{ astring:[anumber, aurl] }
json.stringify() returns
{ "astring":"[number, \"aurl\"]" }
i thought valid json can have arrays values. can have stringify return json string without converting array string? need turn whole js object straight string, without modification.
is there better way this? i've been looking around suggests using json.stringify if want object string, , no 1 has raised problem.
edit: quick responses. here how created js object, please let me know if messed , how!
cookie = {}; // producturl string, timer number, imagesrc url string cookie[producturl] = [timer, imagesrc]; // then, stringified cookie newcookie = json.stringify(cookie);
if relevant, setting actual cookie's value resulting json string, in order access in set of functions. setting cookie's value uri encoding of own, i've been grabbing value of newcookie in chrome console , returns array string.
if object you're trying stringify has tojson
function, called json.stringify
. have external library that's adding array.prototype.tojson.
for example, old version (1.6) of prototype js "conveniently" add you.
prototype 1.6.1:
alert(json.stringify([1, 2, 3]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/prototype/1.6.1/prototype.min.js"></script>
whereas newer version not.
prototype 1.7.2:
alert(json.stringify([1, 2, 3]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/prototype/1.7.2/prototype.min.js"></script>
you try deleting array.prototype.tojson
see if that's what's causing problem. if is, might want upgrading/deprecating libraries in code weird things that.
prototype 1.6.1 (after deleting tojson
)
delete array.prototype.tojson; alert(json.stringify([1, 2, 3]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/prototype/1.6.1/prototype.min.js"></script>
Comments
Post a Comment