I don't really know if this is "a thing" for anyone else, but I have a whole bunch of JSON data that comes down from a provider as a multi-level object and all the string data is escaped (as it should be). I got tired having to unescape() each string value as I went to use it, so I went looking for something someone else had posted online about doing a "deep unescape" and didn't find it.
Given that I didn't find other people asking about it or getting answers, I have to assume that:
a) There's a parameter somewhere I've missed that does it for you
b) There's a function out there that everyone else knows about but me
c) Nobody else has bothered to do this, and I'm just brilliant.
Option "C" seems particularly unlikely given that I don't do anywhere near as much javascript as many other people, but if this turns out to be useful to anyone, here you go.....
function deepUnescapeJSON(object) {
/* created by Andrew Pollack 7/19/2013 */
/* This is a recursive function which will traverse a JSON object */
/* and unescape all the string properties. I have no idea if it */
/* would work on other objects, or even all kinds of JSON data */
/* use it at your own risk. It seems to work for me. */
var val ;
for(var propName in object){
val = object[propName]; // just in case there's some other type of property
if(typeof( object[propName] ) == "string") val = unescape(object[propName]);
if(typeof( object[propName]) == "object") val = deepUnescapeJSON(object[propName]);
object[propName] = val ;
}
return object;
}
Comment Entry |
Please wait while your document is saved.
worked for me.