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 lik开发者_开发知识库e 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.
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);
Windows Phone 7's IE is based on IE9 and as such should support JSON.stringify
. However, as Microsoft's documentation clearly states, JSON.stringify is not available if you are in a quirks mode:
Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards.
Make sure your HTML document starts with <!DOCTYPE html>
. Alternatively, you can use a plain-JavaScript JSON serializer, such as json2.js.
I have no experience with windows phones but I suspect that JSON.stringify is not supported by the browser on the device. This is true of older browsers e.g. ie6 - in which case one work around is to use JSON.js from Douglas Crockford...
https://github.com/douglascrockford/JSON-js
json2.js: This file creates a JSON property in the global object, if there isn't already one, setting its value to an object containing a stringify method and a parse method. The parse method uses the eval method to do the parsing, guarding it with several regular expressions to defend against accidental code execution hazards. On current browsers, this file does nothing, prefering the built-in JSON object.
精彩评论