JSON format
1. The value of a composite type can only be an array or an object , not a function, a regular expression object, or a Date object .
2. There are only four values for simple types: string , numeric (must be expressed in decimal ), boolean , and null(Cannot use NaN, Infinity, -infinity and undefined).
3. The string must be in double quotation marks and cannot use single quotation marks .
4. The key name of the object must be enclosed in double quotation marks .
5. After the last member of an array or object, you cannot add commas .
empty arrays and empty objects are qualified JSON values, and null itself is also a qualified JSON value
Json.stringify ()
Used to convert a value to a string. The string conforms to the JSON format and can be restored by the Json.parse method.
Json.stringify (' abc ')//"" ABC "" Json.stringify (1)//"1" json.stringify (FALSE)//"false" json.stringify ([])//"[]" Json.stringify ({})//"{}" json.stringify ([1, "false", false]) //' [1, ' false ', false] ' json.stringify ({name: ' Zhang San '}) //' {' name ': ' Zhang San '}
The above code turns the various types of values into JSON strings.
It is important to note that for strings of the original type , the conversion result is enclosed in double quotation marks .
Json.stringify (' foo ') = = = "Foo"//falsejson.stringify (' foo ') = = = "\" foo\ ""//True
String Foo, which was turned into "foo" "
This is because when you restore in the future, double quotes let the JavaScript engine know that Foo is a string, not a variable name.
If the original object has a member that has a value of undefined, a function, or an XML object , the member is filtered.
var obj = { a:undefined, b:function () {}}; Json.stringify (obj)//"{}"
The properties of the object obj a are undefined , and the b property is a function, and the result is JSON.stringify filtered.
If the members of the array are undefined , functions, or XML objects , the null values are converted.
var arr = [Undefined, function () {}]; Json.stringify (arr)//"[Null,null]"
arrthe members of the array are the undefined and functions, and they are all turned into null .
the regular object is turned into an empty object.
Json.stringify (/foo/)//"{}"
JSON.stringifyMethod ignores the object's non-traversal properties .
Note: Non-traversal properties are still not understood, Baidu half a day is still do not know ....
var obj = {};object.defineproperties (
obj, { ' foo ': { value:1, enumerable:true }, ' bar ': { value:2, enumerable:false }} ); Json.stringify (obj); "{" foo ": 1}"
baris an obj object's non-traversal property, the JSON.stringify method ignores this property.
Json.parse ()
JSON.parsemethod is used to convert a JSON string into an object .
Json.parse (' {} ')//{}json.parse (' true ')//Truejson.parse (' "foo")//"foo" Json.parse (' [1, 5, ' false '] ')//[1, 5, "false "]json.parse (' null ')//Nullvar o = Json.parse (' {" name ":" Zhang San "} '); O.name//Zhang San
If the string passed in is not a valid JSON format , the JSON.parse method will error .
Json.parse ("' String '")//Illegal single quotes//syntaxerror:unexpected token illegal
The double-quote string is a single-quote string, because the single-quote string does not conform to JSON format, so an error is given.
To handle parsing errors, you can JSON.parse place the method in a try...catch code block.
JSON.parseThe method can accept a handler function, which is similar in usage to the JSON.stringify method.
function f (key, value) { if (key = = =) { return value; } if (key = = = ' A ') { return value + Ten; }} var o = json.parse (' {"A": 1, "B": 2} ', f); O.A//11O.B//undefined
Json.stringify (), Json.parse ()