Parsing JSON Boolean Values?
Been on this one for hours. When parsing a JSON response string from an ajax call, the 'true/false' values are put into an array as a string (not boolean). The json string does not have boolean values enclosed in quotes. Also, return header is application/x-json.
I've tried prototype, crockford, byteson and plain straight eval(responseText) but a boolean value returned has a string type. Is there any way to make a boolean value have a boolean type in JS?
You can coerce a string to return another type with a string method-
if(/^(true|false|null|undefined|NaN)$/i.test(this)) return eval(this);
if(parseFloat(this)+''== this) return parseFloat(this);
Last edited by mrhoo; 05-05-2007 at 07:26 PM.
Are you saying that you have an array of strings in which each element contains either 'true' or 'false', and you want them all converted to booleans? If so, I think this should do it:
Originally Posted by racecar789
for(var i=0; i<responseArray.length; i++)
Where used, return should be executed unconditionally and always as the last statement in the function.
That's my signature, it's not part of the damn post!
Hey that does it, thanks a bunch mrhoo and Logic Ali!
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)