2

Possible Duplicate:
JSON.stringify() bizarreness

OK, so I use the various JSON methods (stringify and parse) but have recently run across a page where they don't behave correctly. I am using Chrome version 23.0.1271.91 m

As an example, if I throw the following in to the console:

var x = [{"color":"red"},{"name":"ryan"}];
console.log('original object = ', x);
var y = JSON.stringify(x);
console.log('stringified = ', y);
var w = JSON.parse(y);
console.log('parsed = ', w);

I usually get back this:

original object = [Object, Object]
stringified =  [{"color":"red"},{"name":"ryan"}]
parsed = [Object, Object]

enter image description here

But on one page in particular I get the following instead:

original object = [Object, Object]
stringified =  "[{\"color\": \"red\"}, {\"name\": \"ryan\"}]"
parsed =  [{"color": "red"}, {"name": "ryan"}]

enter image description here

Where parsed is a string with the value of [{"color": "red"}, {"name": "ryan"}] rather than an object with those values/properties.

I'm just not really sure how to go about solving this issue. Obviously, something is going wrong in the serialization. This is in an environment where I don't have a lot of control over what other scripts are running (it's a plugin) so the solution can't really involve disabling other scripts which might be causing interference because I may not know about them and that may cause additional problems.

Community
  • 1
  • 1
Ryan Elkins
  • 5,731
  • 13
  • 41
  • 67
  • "stringify and parse mostly" ... what do you mean "mostly?" Those are the only two members of JSON in stock implementations. If you are using others, please let us know which JSON implementation you are using. – Dagg Nabbit Dec 06 '12 at 22:42
  • The other day I was playing around with some serialize and deserialize methods but I'm now not sure where those where coming from. I'll edit the question to remove the ambiguity as I am definitely talking about the standard JSON.stringify and JSON.parse methods. – Ryan Elkins Dec 06 '12 at 23:09
  • 1
    It turns out your problem is prototypejs. The same issue has been asked about before: http://stackoverflow.com/questions/710586/json-stringify-bizarreness – Mark Amery Dec 06 '12 at 23:39
  • I was going to say, is that page including a library that overwrites the JSON object? – Brian Cray Dec 06 '12 at 23:40

0 Answers0