jQuery.parseJSON( json )Returns: String or Number or Object or Array or Booleanversion deprecated: 3.0, removed: 4.0
Description: Takes a well-formed JSON string and returns the resulting JavaScript value.
-
version added: 1.4.1jQuery.parseJSON( json )
-
jsonType: StringThe JSON string to parse.
-
$.parseJSON
is deprecated. To parse JSON strings use the native JSON.parse
method instead.
-
"{test: 1}"
(test does not have double quotes around it). -
"{'test': 1}"
('test' is using single quotes instead of double quotes). -
"'test'"
('test' is using single quotes instead of double quotes). -
".1"
(a number must start with a digit;"0.1"
would be valid). -
"undefined"
(undefined
cannot be represented in a JSON string;null
, however, can be). -
"NaN"
(NaN
cannot be represented in a JSON string; direct representation ofInfinity
is also not permitted).
$.parseJSON( '{ "testing":"1\t2\n3" }' )
will throw an error in most implementations because the JavaScript parser converts the string's tab and newline escapes into literal tab and newline; doubling the backslashes like "1\\t2\\n3"
yields expected results. This problem is often seen when injecting JSON into a JavaScript file from a server-side language such as PHP.
JSON.parse
, jQuery uses it to parse the string. For details on the JSON format, see https://json.org/<.
$.parseJSON
returned null
instead of throwing an error if it was passed an empty string, null
, or undefined
, even though those are not valid JSON.
Example:
1
2
|
|