Latest web development tutorials

jQuery.parseJSON () method

jQuery Misc Methods jQuery Misc Methods

Examples

Parse a JSON string

$ (Function () { var . obj = jQuery parseJSON ( '{ "name": "John"}'); alert ( obj. name === "John" );})

try it"

Definition and Usage

JSON string $ .parseJSON () function is used to match the standard format into the corresponding JavaScript object.

Note: Incoming malformed JSON string may result in an exception. For example, the following invalid JSON string:


"{test: 1}"	
//test是属性名称,必须加双引号

"{'test': 1}"	
//test是属性名称,必须用双引号(不能用单引号)

"'test'" 
//test是属性名称,必须用双引号(不能用单引号)

".1" 
//number 必须以数字开头; "0.1" 将是有效的

"undefined"	
//undefined 不能表示一个 JSON 字符串; null可以

"NaN" 
//NaN 不能表示一个 JSON 字符串; 用Infinity直接表示无限也是不允许的

JSON standard does not allow "control characters" such as tabs or line breaks, for example:


// 多数情况下,它会抛出一个错误,因为JS解析器会将字符串中的\t或\n等转义直接视作字面值,起到Tab或换行的效果。
$.parseJSON('{"testing":"1\t2\n3"}')   

Correct wording should be as follows (use two backslashes to avoid being directly JS parser escape \ t or \ n):

$.parseJSON('{"testing":"1\\t2\\n3"}')  

Note: Before jQuery 1.9 (excluding 1.9): If you pass an empty string, null or undefined, the function will return null, instead of throwing an error, even though it is not a valid JSON string.


grammar

$ .parseJSON (Json)

parameter description
json String types need to be resolved and converted to JS object JSON format string


jQuery Misc Methods jQuery Misc Methods