Friday, 15 April 2011

jquery - Why is one number valid json? -


$ ParseJSON ("1") returns 1 . I hope this will throw an error because it does not look like a valid JSON of the form:

  {"firstname": "John"}   

Why Correcting 1 correctly? Is there anyway to throw an error instead?

parsing a number

to better parse numbers Can be used. It will return a number on success and NaN (not a number) otherwise

  var a = parseInt ('23 '); IsNan (a); // wrong var b = parseInt ('ab'); IsNan (b); // True   

Why this gives in jQuery 1

If you keep an eye on the source of the jQuery method, then it will become very fast.

  1. It will check if there is basic support for JSON.parse.
  2. If not, it will create an anonymous function (with string body) which just gives the data contained in the JSON string and calls it.

    So if your case moves 2. is executed, it will only return 1 , even if it is not a real JSON. Update . Therefore, regardless of implementation, you always get the same result.

    Library on display:

      Parsegas: function (data) {if (! Data = data type =! = "String") {return tap; } // Be sure that the leading / backward white location has been removed (IE can not handle it) data = jQuery.trim (data); // First try to parse the original JSON parser if (window.JSON & window.JSON.parse) {return window.JSON.parse (data); } // Make sure that incoming data is the logic taken from real JSON / http://json.org/json2.js (rvalidchars.test (data location (rvalidescape, "@"). Replace (rvalidtokens, " ] "Return" (Returns the JSON data only) JQuery.error ("invalid JSON:" + data); return (rvalidbraces, "")) {return (new function ("return" + data)) },    

No comments:

Post a Comment