Search code examples
javascriptjqueryjsoncookiesstringify

javascript / jquery - JSON.stringify not working in windows phone 7


I'm testing a mobile website on windows phone 7 and the JSON.stringify function is not working. Does anyone know why this might be happening or what I can do to solve this? I am stringifying an array in order to store it in a cookie. This is my code:

vString = JSON.stringify(vehicleArray);
alert ('this alert will never execute');
$j.cookie('vehicleCookie', vString, { expires: 7, path: '/' }); //Store all the vehicles again to the cookie

Any code beyond the vString line ceases to execute.

The vehicleArray looks like this, if logged:

["145", "273", "241", "553", "490", "380"]

I can't provide much information on what is or isn't working because windows phone doesn't have any sort of debugger that I'm aware of, and the code works fine in ie7.


Solution

  • You can extend jQuery to give it a jQuery.stringify() function.

    It's minified to conserve space:

    jQuery.extend({stringify:function(a){var c=typeof a;if(c!="object"||a===null)return c=="string"&&(a='"'+a+'"'),String(a);else{var d,b,f=[],e=a&&a.constructor==Array;for(d in a)b=a[d],c=typeof b,a.hasOwnProperty(d)&&(c=="string"?b='"'+b+'"':c=="object"&&b!==null&&(b=jQuery.stringify(b)),f.push((e?"":'"'+d+'":')+String(b)));return(e?"[":"{")+String(f)+(e?"]":"}")}}});
    

    So just include this at the top of your file and change your problematic line to this:

    vString = jQuery.stringify(vehicleArray);