isJsonString('{ "Id": 1, "Name": "Coke" }')
should be true and
isJsonString('foo')
isJsonString('<div>foo</div>')
should be false.
I’m looking for a solution that doesn’t use try/catch because I have my debugger set to "break on all errors" and that causes it to break on invalid JSON strings.
A comment first. The question was about not using
try/catch.If you do not mind to use it, read the answer below.
Here we just check a
JSONstring using a regexp, and it will work in most cases, not all cases.Have a look around the line 450 in https://github.com/douglascrockford/JSON-js/blob/master/json2.js
There is a regexp that check for a valid JSON, something like:
EDIT: The new version of json2.js makes a more advanced parsing than above, but still based on a regexp replace ( from the comment of @Mrchief )