1

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\"]""

4
  • Any chance that you did turn it into JSON twice? Commented Feb 17, 2012 at 9:39
  • can you try console.dir(n) and see if there is any difference bt the two? Commented Feb 17, 2012 at 9:42
  • @Gumbo I don't see how if I just run jQuery.toJSON([]). Commented Feb 17, 2012 at 9:53
  • @ustun yes, I showed the difference in the second box. Same functions names but different implementions. Commented Feb 17, 2012 at 9:55

2 Answers 2

1

The extra quotes are caused by the

Array.prototype.toJSON

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 use

delete Array.prototype.toJSON // remove toJSON for all Arrays
//or
delete n.toJSON // remove toJSON for specific Array

before you do the jQuery.toJSON, that should work!



As another suggestion, it is better to use

JSON.stringify(object)

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 the JSON.stringify() function.

For JSON.stringify(), see https://developer.mozilla.org/En/Using_native_JSON for more info.

Sign up to request clarification or add additional context in comments.

10 Comments

I did, same problem. Thanks for the idea!
In what browser + situation do you get the extra quotes?
Hmm, JSON is meant for objects, not for arrays (although they are technically also objects). If you want to JSON-ify arrays, you should first create an object from that array, thén stringify... Could you try that?
JSON.stringify( {a: ["test"]} ); => "{"a":"[\"test\"]"}". The object is ok but the array is still wrong.
Thanks, I found a even better solution: n.toJSON = null works the same way but does not affect's other arrays. Maybe you can add that to your answer.
|
0

My dirty hack for this is:

function cleanJson(s)
{
    if (s[0] == '"')
    {
        s = s.substring(1, s.length-1).replace(/\\"/g,'"');
    }
    return s;
}

Does the job, but I am still looking for a cleaner solution.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.