Dave's Brain

Browse - programming tips - javascript safely parse json in any browser

Date: 2009dec22
Language: javaScript

Q.  How can I safely parse JSON in any browser?

A.  Many browsers have a JSON.parse() function which is the best thing
to use.  But not all.  So use this function:

function parseJson(json) {
	var	out;

	try
	{
		if (typeof JSON == 'object' && typeof JSON.parse == 'function') {
			 out = JSON.parse(json);
		}
		else {
			out = eval('('+json+')');
		}
	}
	catch(e) {
		// do nothing
	}

	return out;
}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2010, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.