I am running
var n = [];
jQuery.toJSON( n );
On one page I get "[]" on the other ""[]"". On both pages I run the same jQuery version with a toJson Plugin.
In Firefox DOM I can see both arrays have the same function names, but … different:
all b(B, A)
any j(B, A)
all all(iterator, context)
any any(iterator, context)
I guess there are some Array.prototype functions before my script. That causing the arrays to be different. I can’t change the other code I have to somehow deal with this.
I tried new Array() and jQuery.makeArray(n), still same result. I actually don’t care that the arrays are not equal but how do I get the same JSON code for this? It’s getting worse if I have strings in the array: ""[\"a\", \"b\"]""
The extra quotes are caused by the
function that is defined by the Prototype library and possibly other libraries as well. This function is called by
jQuery.toJSON(or JSON.Stringify() for that matter) and produces the extra quotes. If you would usebefore you do the
jQuery.toJSON, that should work!As another suggestion, it is better to use
instead of
jQuery.toJSON. It is supported natively in most browsers. If you want to be sure it works everywhere, use https://github.com/douglascrockford/JSON-js, it is the basis used for theJSON.stringify()function.For
JSON.stringify(), see https://developer.mozilla.org/En/Using_native_JSON for more info.