The difference between jquery's JSON.parse JSON.stringify and parseJSON methods

1.JSON.parse(str) is used to parse a json object from a string, such as

var str = '{"name":"huangxiaojian","age":"23"}'

result:

JSON.parse(str)

Object
  1. age"23"
  2. name"huangxiaojian"
  3. __proto__Object

Note : Single quotes are written outside {}, and each attribute name must be double quoted, otherwise an exception will be thrown.

 

2.JSON.stringify(a) is used to parse a string from an object, such as

var a = {a:1,b:2}

result:

JSON.stringify(a)

"{"a":1,"b":2}"

 

3.jQuery.parseJSON() The function is used to convert a well- formed JSON string into its corresponding JavaScript object .

The so-called "well-formed" means that the specified string must conform to the strict JSON format. For example, the attribute name must be enclosed in double quotation marks, and the string value must also be enclosed in double quotation marks.

a: A JS exception will be thrown if a JSON string that is not "well-formed" is passed in, for example: the following strings are all JSON-formatted, but they are not well-formed JSON strings (thus throwing an exception):

// The following are all string values, and the quotation marks on both sides are omitted to facilitate the display of the content 

{ id : 1 } // id is the attribute name and must be double quoted { 'id' : 1 } // id is the attribute name, Double quotation marks must be used (single quotation marks cannot be used) { "name" : 'CodePlayer' } // The value of the name attribute is a string, and double quotation marks must be used (single quotation marks cannot be used)

b: The JSON standard does not allow "control characters" in strings, for example: a Tab or a newline.

$ .parseJSON ( '{"name": "Code\tPlayer\n"}' ) ; // Most of the time, it will throw an error because JS parser will disregard \t or \n etc in the string Escape is directly regarded as a literal value, which has the effect of Tab or newline. //The correct way to write it should be as follows (use two backslashes to avoid escaping \t or \n directly by the JS parser): $ .parseJSON ( '{"name": "Code\\tPlayer\\n"} ' ); 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325651796&siteId=291194637