3

I tried that:

var c = $.parseJSON(something here)

and I control that:

c === undefined

This works however it throws error while trying to parse an invalid JSON string. I don't want it throw that error.

Any advices?

kamaci
  • 72,915
  • 69
  • 228
  • 366
  • is the error halting the execution of your scripts or just complaining about the format? – jondavidjohn Sep 27 '11 at 18:54
  • check http://stackoverflow.com/questions/2313630/ajax-check-if-a-string-is-json http://stackoverflow.com/questions/3945499/how-to-know-if-return-data-is-json-or-string-in-jquery-and-php – Mithun Satheesh Sep 27 '11 at 19:07
  • This shouldn't be a duplicate of that question mentioned above. This question asks the Jquery version and the other question is asking for plain javascript. **JQuery and javascript are not the same thing** – Kellen Stuart Jul 19 '17 at 17:36
  • I think so @KolobCanyon. – kamaci Jul 23 '17 at 13:04

1 Answers1

18

It's generally considered bad practice to suppress/ignore errors, instead why not use a try-catch block to capture the exception and do something with it:

try {
  var c = $.parseJSON(something here);
}
catch (err) {
  // Do something about the exception here
}

If you really don't need to do anything about the exception at least put a comment to that effect in your try-catch block, it'll make your code more readable when you come back to it later.

Scott Lawrence
  • 6,993
  • 12
  • 46
  • 64
Clive
  • 36,918
  • 8
  • 87
  • 113